Reputation: 2046
I am trying to format a string in the output differently than the way it is already formatted within my program. It's a pizza ingredient menu and in the bulk of the program whenever it's outputted it is formatted right with a "\n" between the ingredients so as to create a list "feel". However, in the final menu where the users choice is displayed, I want to also put the ingredient list because the user can customize which ingredients are on their pizza. This is difficult with a vertical list of the ingredients because it can make the final output really long if the user has more than one pizza. I'm trying to find out if there's a way I can take the same string that was formatted for vertical display and display it horizontally with a comma between the ingredients instead of "\n". Any ideas? Or should I just create a new string that's formatted the way I want with the information I need?
String i1 = "Pepperoni", i2 = "Sausage", i3 = "Canadian Bacon";
String ingredients = i1 + "\n" + i2 + "\n" + i3 + "\n";
System.out.print(ingredients);
System.out.printf("Your pizza includes only these ingredients: %s", ingredients);
The first System.out.print(ingredients) is similar to the code that I have that prints the ingredients vertically throughout the program so that it's easily readable to the user.
The second System.out.print(...) is what I'm trying to format so that the list of ingredients displays horizontally.
Upvotes: 0
Views: 11642
Reputation: 718718
I'd have thought that this was clearer:
String i1 = "Pepperoni", i2 = "Sausage", i3 = "Canadian Bacon";
System.out.printf("%s\n%s\n%s\n", i1, i2, i3);
System.out.printf("Your pizza includes ingredients: %s %s %s", i1, i2, i3);
The point is, if you are going to use printf
at all, you may as well make use of its ability to join strings together.
From your comment, I take it that the number of ingredients is variable. If that's the case, using printf
for formatting the "list" (like I did above) won't cut it. Instead you need a helper method like this:
public static String join(String separator, Object[] things) {
StringBuilder sb = new StringBuilder();
for (Object thing : things) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(thing);
}
return sb.toString();
}
Then ...
Object[] ingredients = new Object[]{i1, i2, i3}; // ... or a dynamic sided array
System.out.print(join("\n", ingredients));
System.out.println("Your pizza includes ingredients: " + join(" ", ingredients));
I claim that this is more general (works with variable size arrays), easier to understand, and possibly more efficient than other suggestions.
(The join
helper method exists in various guises in a number of class libraries; e.g. in the Apache Commons StringUtils
class. )
Upvotes: 1
Reputation: 24641
replacing \n with , is rookie programmer style and will come back to bite you soon. try this instead for the comma part (and do the \n part manually like you are already doing):
String [] ingredients = {"Pepperoni", "Sausage", "Canadian Bacon"};
String almost_there = Arrays.deepToString(ingredients);
String there = almost_there.substring(1, almost_there.length() - 1);
System.out.printf("Your pizza includes ingredients: ", there);
Upvotes: 0
Reputation: 469
String i1 = "Pepperoni", i2 = "Sausage", i3 = "Canadian Bacon";
String ingredients = i1 + "\n" + i2 + "\n" + i3;
System.out.println(ingredients);
System.out.println("Your pizza includes only these ingredients: " + ingredients.replace("\n", ", ")
Upvotes: 1
Reputation: 27326
String s = "hi\nthere";
s.replaceAll("\n", ","); // (returns "hi,there")
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
Upvotes: 1