java.lang.StringBuilder
System.out.println(new StringBuilder("I like")
.append(cake)
.append(" and I eat ")
.append(cakeNumber)
.append(" blah blah prolonging this string because ")
.append(whyNot)
.append(" and so on ")
.append(number)
.append(".")
.toString());
java.text.MessageFormat
System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah prolonging this string because {2} and so on {3}.",
cake, cakeNumber, whyNot, number));
I kind of like it with a static import, like this:
import static java.text.MessageFormat.format;
System.out.println(format("I like {0} and I eat {1} blah blah prolonging this string because {2} and so on {3}.",
cake, cakeNumber, whyNot, number));
java.util.Formatter (also known as String.format)
System.out.printf("I like %s and I eat %d blah blah prolonging this string because %s and so on %f.%n",
cake, cakeNumber, whyNot, number);
You have lots of syntactic choice at your disposal here (Just to list some, there is probably more):
System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))
%n represents the system's specific line break character. It is required because printf does not insert a line break after the operation automatically.
Multiline string concatenation
System.out.println("I like " + cake + " and I eat " +
cakeNumber + " blah blah prolonging this string because" +
whyNot + " and so on " + number + ".");
Answer from randers on Stack Overflowjava - Formatting a long system.out.print containing many strings & integers - Stack Overflow
Java - String Formatting
[Java] How can I convert a string of long text into IPA?
String.format() is 3x faster in Java 17
Videos
java.lang.StringBuilder
System.out.println(new StringBuilder("I like")
.append(cake)
.append(" and I eat ")
.append(cakeNumber)
.append(" blah blah prolonging this string because ")
.append(whyNot)
.append(" and so on ")
.append(number)
.append(".")
.toString());
java.text.MessageFormat
System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah prolonging this string because {2} and so on {3}.",
cake, cakeNumber, whyNot, number));
I kind of like it with a static import, like this:
import static java.text.MessageFormat.format;
System.out.println(format("I like {0} and I eat {1} blah blah prolonging this string because {2} and so on {3}.",
cake, cakeNumber, whyNot, number));
java.util.Formatter (also known as String.format)
System.out.printf("I like %s and I eat %d blah blah prolonging this string because %s and so on %f.%n",
cake, cakeNumber, whyNot, number);
You have lots of syntactic choice at your disposal here (Just to list some, there is probably more):
System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))
%n represents the system's specific line break character. It is required because printf does not insert a line break after the operation automatically.
Multiline string concatenation
System.out.println("I like " + cake + " and I eat " +
cakeNumber + " blah blah prolonging this string because" +
whyNot + " and so on " + number + ".");
Try printf
For example, you could write
System.out.printf("I like %s and I eat %d blah blah prolonging this string because %s and so on %0.f.\n", cake, cakenumber, whyNot, number);
Also note that many IDEs (like Eclipse) will allow you to easily span strings across many lines. But the advantage of printf is that you separate the string from the variables, and also allows for better control over the display of numbers. For example, notice the %.0f. In general you can specify how much space the value should take up including padding and how many decimal places to use.
/**
* Call String.format with three integer arguments and
* display them in reverse order.
* @param num1 50
* @param num2 60
* @param num3 70
* @param num4 80
*
* Output:
* Fourth: 50
* Third: 60
* Second: 70
* First: 80
*/
public static void reverseNums(add a parameter list here){ //#1
String reverse = String.format(add a format string here); //#2 System.out.println(reverse);
}
We are supposed to reverse these numbers, however my question is how is it possible to pass the numbers as arguments into the String.format() method, can't I only pass in Strings? A nudge in the right direction would be a huge help. Thanks