How to format Output and Input in Java – Tec Geeks
In this post, you will learn to:
- State the different format specifiers in Java.
- Identify the methods for accepting formatted input.
- List the various escape sequence characters in Java.
Whenever an output is to be displayed on the screen, it needs to be formatted. The formatting can be done with the help of format specifiers in Java. The printf() method introduced in J2SE 5.0 can be used to format the numerical output to the console.
The table below lists some of the format specifiers in Java.
|%d||Result formatted as a decimal integer|
|%f||Result formatted as a real number|
|%o||Results formatted as an octal number|
|%e||Result formatted as a decimal number in scientific notation|
|%n||Result is displayed in a new line|
The following code demonstrates the use of various format specifiers.
int i = 55/22; // decimal integer System.out.printf ("55/22 = %d %n", i); // Pad with zeros double q = 1.0/2.0; System.out.printf ("1.0/2.0 = %09.3f %n", q); // Scientific notation q = 5000.0/3.0; System.out.printf ("5000/3.0 = %7.2e %n", q); // Negative infinity q = -10.0/0.0; System.out.printf ("-10.0/0.0 = %7.2e %n", q); // Multiple arguments //Pi value, E–base of natural logarithm System.out.printf ("pi = %5.3f, e = %5.4f %n", Math.PI,Math.E);
55/22 = 2 21.0/55.0 = 00000.500 5000/3.0 = 1.67e+03 -10.0/0.0 = -Infinity pi = 3.142, e = 2.7183
In the snippet, ‘%09.3f’ indicates that there will be total 9 digits including decimal point and three places of decimals. If the number of digits is less than 9, then it will be padded with zeroes. If ‘0’ is omitted from ‘%09.3f’, then it will be padded with spaces.
The Scanner class allows the user to read values of various types. To use the Scanner class, pass the InputStream object to the constructor.
Scanner input = new Scanner(System.in);
Here, input is an object of Scanner class and System.in is an input stream object. The table below lists the different methods of the Scanner class that can be used to accept numerical values from the user.
|nextByte()||Returns the next token as a byte value|
|nextInt()||Returns the next token as an int value|
|nextLong()||Returns the next token as a long value|
|nextFloat()||Returns the next token as a float value|
|nextDouble()||Returns the next token as a double value|
The following code demonstrates the Scanner class methods and how they can be used to accept values from the user and finally print the output.
//creates an object and passes the inputstream object Scanner s = new Scanner(System.in); //Accepts values from the user byte byteValue = s.nextByte(); int intValue = s.nextInt(); float floatValue = s.nextFloat(); long longValue = s.nextLong(); double doubleValue = s.nextDouble(); System.out.println("Values entered are: "); System.out.println(byteValue + " " + intValue + " " + floatValue + " " + longValue + " " + doubleValue);
Values entered are: 121 2333 456.789 456 3456.876
A package is a collection of classes.
The Scanner class belongs to the java.util package. The Scanner looks for tokens in the input. A token is a series of characters that ends with delimiters. A delimiter can be a whitespace (default delimiter for tokens in Scanner class), a tab character, a carriage return, or the end of the file. Thus, if we read a line that has a series of numbers separated by whitespaces, the scanner will take each number as a separate token.
Constructors are used to create an instance of a class.
Characters are read from the keyboard by using System.in.
An escape sequence is a special sequence of characters that is used to represent characters, which cannot be entered directly into a string. For example, to include tab spaces or a new line character in a line or to include characters that otherwise have a different connotation in a Java program (such as or “), escape sequences are used. An escape sequence begins with a backslash character (), which indicates that the character (s) that follows should be treated in a special way. The output displayed by Java can be formatted with the help of escape sequence characters.
The table below displays the various escape sequences in Java.
|t||Horizontal Tab character|
|n||New line character|
|’||Single quote marks|
|r||Carriage Return character|
|”||Double quote marks|
|xxx||Character corresponding to the octal value xxx, where xxx is between 000 and 0377|
|uxxx||Unicode corresponding with encoding xxxx, where xxxx is one of four hexadecimal digits. Unicode escapes are distinct from the other escape types.|
The following code demonstrates the use of escape sequence characters.
// use of tab and new line escape sequences System.out.println("Java t Programming n Language"); // printing Tom "Dick" Harry string System.out.println("Tom "Dick" Harry");
Java Programming Language Tom "Dick" Harry
To represent a Unicode character, Unicode u escape sequence can be used anywhere in a Java program. A Unicode character can be represented using hexadecimal or octal sequences.
The following code demonstrates the use of xxx and uxxx escape sequence types.
// Print 'Hello' using hexadecimal escape sequence characters System.out.println("u0048u0065u006Cu006Cu006F" + "!n"); // Print 'Blake' using octal escape sequence character for 'a' System.out.println("Bl141ke"2007" ");
The output of Code Snippet 6 is as follows:
NOTE: The hexadecimal escape sequence starts with u followed by 4 hexadecimal digits. The octal escape sequence comprises 3 digits after back slash. For example, “xyy” where, x can be any digit from 0 to 3 and y can be any digit from 0 to 7.