How to Format Strings in Java Basic Syntax
Java provides various formatting approaches to text output. Using the format method of the string class, you can create string objects with formatted content, using this content to output information to your program users. Creating formatted strings typically is not difficult, so even beginners to the language can achieve the task quickly. The string-format method provides a good level of flexibility in terms of string output, so your main task will be to choose what you want your formatted string to contain.
Instructions
-
-
1
Prepare the data you want to format within your strings. String formatting allows you to format text strings with numeric and other content. The following sample Java code demonstrates creating a few numeric variables.
int myInt = 3;
double myDouble = Math.sqrt(5);
int otherInt = 31;
These examples are for demonstration, but in your own code you can use any numeric types you like, as well as dates, times and characters among other options.
-
2
Carry out the formatting operation in your program. The following sample code demonstrates calling the format method on the string class, passing it some text as well as format specifiers indicating the formatting types to be reflected in the string output.
String.format("myInt: %d myDouble: %f otherInt: %x");
This code is not yet complete, but demonstrates the first part of the process. The format specifiers appear after the percentage sign to instruct Java to treat those sections of the string as formatted content, indicating decimal, floating point and hexadecimal formatting.
-
-
3
Pass the format method your variables as additional parameters. When Java encounters the format specifiers in your code, it will look for variables or values to include. The following extended code demonstrates including the three variables as method parameters.
String.format("myInt: %d myDouble: %f otherInt: %x", myInt, myDouble, otherInt);
This code includes the variables in the order reflected within the first string parameter to the method. The resulting string will contain the text and the values within each variable, formatted according to the specifiers listed.
-
4
Store your new string in a variable. The string format method returns a new string value containing the formatted characters requested. Alter your code to store the result in a string variable as follows.
String formattedString = String.format("myInt: %d myDouble: %f otherInt: %x", myInt, myDouble, otherInt);
The Java language provides formatting facilities for output streams as well as strings, but the advantage to using the string-class method is that you acquire a reference to a new string variable, which you can then use at any subsequent point in your program.
-
5
Save your Java file, compile and run your program to test it. You can include the following simple line of code to output your new string to the standard output console.
System.out.println(formattedString);
This is a quick way to test your processing. If the program does not function as you expected it to, check your code again, including your variable values.
-
1
Tips & Warnings
Experiment with the options within the format method of the string class, as there are lots of different specifiers you can choose from.
If your code only needs to output the formatted string once, there is no need to save it in a variable as this uses up memory resources unnecessarily.
References
Resources
- Photo Credit Photos.com/AbleStock.com/Getty Images