• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginning Programming FAQ  RSS feed

 
Eddie Gerlach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy Thanksgiving, everyone!

I'm running into an issue with a homework problem and could use some guidance....I've included my code and the question for further review and assistance...

Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can't agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.

Here's my code I've written on NetBeans:

import java.util.Scanner;

public class ThreePartners {
public static void main(String [] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter three names:");
String name1 = stdin.next();
String name2 = stdin.next();
String name3 = stdin.next();
System.out.print(name1 + ", " + name2 + " and " + name3 + "\n");
System.out.print(name2 + ", " + name3 + " and " + name1 + "\n");
System.out.print(name3 + ", " + name1 + " and " + name2);

}
}

And here's my result....although it says it's successful on NetBeans, I'm not quite sure it's right even though it's "successful", plus MyProgrammingLab through Pearson and Dr. Liang's Intro to Java 9th Ed., Comprehensive isn't happy with the code, either... :/

run:
Enter three names:
Larry
Curly
Moe
Larry, Curly and Moe
Curly, Moe and Larry
Moe, Larry and CurlyBUILD SUCCESSFUL (total time: 13 seconds)

Thanks again for the assistance....this is much harder than I have anticipated, yet I'm having the time of my life and really enjoying this!

Eddie Gerlach
Greenhorn
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, with three names, there a six combinations (not just the three that you shown).

Second, is hardcoding it ...



really a good idea? What if there are four names? Five names? The number of combinations will greatly increase -- and it may be better to code it using loops and such, even though for the case of three names, it is probably easier the way you have it.

Henry
 
Eddie Gerlach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to bother ya'll! I just realized I needed another three lines of code to show all possible combinations of the three names, hence a total of 6 lines of code....talk about feeling sheepish!

Happy Thanksgiving!
 
Eddie Gerlach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

Yes, exactly what i was thinking, what if there's 5, 8 or more names, the combinations would be ridiculous to figure out manually....but I haven't learned that yet....still in Chapter 2....I'm just happy as a clam to be coding without any prior programming experience....here's what it looks like now...

name1 = stdin.next();
name2 = stdin.next();
name3 = stdin.next();
System.out.println(name1 + ", " + name2 + " and " + name3 + "\n");
System.out.println(name2 + ", " + name3 + " and " + name1 + "\n");
System.out.println(name3 + ", " + name1 + " and " + name2 + "\n");
System.out.println(name1 + ", " + name3 + " and " + name2 + "\n");
System.out.println(name2 + ", " + name1 + " and " + name3 + "\n");
System.out.println(name3 + ", " + name2 + " and " + name1);

geez....I'm getting there...slowly! Thanks again!

Eddie Gerlach
Greenhorn
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t use \n. Use printf and the %n tag. Note that using println and \n together will result in two new lines.

You need to go back and find a pencil and paper and write how you intend to print the names in different orders. There is bound to be an algorithm, but you will have to work it out before you try writing any actual code.
 
Eddie Gerlach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,

Thank you for the reply, but I don't think we've covered that yet....it says in the example that I needed to use "(that is, each possible combination is terminated with a newline character)"

I was under the impression that "\n" was the newline character, but felt it was unnecessary because of each new "System.out.print"....attempts to concatenate the result w/o using multiple println's gave me more errors....humph!

At any rate, MyProgrammingLab is now happy with the code and it also works in my NetBeans, too.....hoo-ray Beer!

Eddie Gerlach
Green Greenhorn

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The escape sequence \n gives you a character whose real name is line feed. Even though you pronounce \n “newline”. Lots of people (and books) think it is how you terminate the line, but it might not be. It is on my Linux box, but if I reboot it into Windows7®, it isn’t. If you use println, that adds the correct line terminator depending on your system. More about % tags here and here; one of those links tells you about %n.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!