Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.
To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.
Input Format
Every line of input will contain a String followed by an integer.
Each String will have a maximum of alphabetic characters, and each integer will be in the inclusive range from to .
Output Format
In each line of output there should be two columns:
The first column contains the String and is left justified using exactly characters.
The second column contains the integer, expressed in exactly digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.
Sample Input
java 100
cpp 65
python 50
Sample Output
================================
java 100
cpp 065
python 050
================================
Explanation
Each String is left-justified with trailing whitespace through the first characters. The leading digit of the integer is the character, and each integer that was less than digits now has leading zeroes.
Problem solution:
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x=sc.nextInt(); //Complete this line System.out.printf("%-15s%03d%n",s1,x); } System.out.println("================================"); } }
Explanation :
- Here %-15s = - is a flag here that will be responsible for left justification of characters within 15 spaces.
- %03d = Here 0 is a flag that will add 0 to integer values if the value is not 3 digits.
More details :
It is also an overloaded method of the PrintStream class.
syntax:-
System.out.printf(string); (the string parameter is simple as the printIn() method)
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);
System.out.printf(string); (the string parameter is simple as the printIn() method) System.out.printf(format, arguments); System.out.printf(locale, format, arguments);
Format
To specify the formatting rules we use the format parameter. This string is composed of literals and format specifiers. Rules start with the % character. Arguments are required only if there are format specifiers in the format string. Format specifiers include flags, width, precision, and conversion characters in the below sequence:-
%[flags][width][.precision]conversion-character
Specifiers in the brackets are optional.
Conversion Characters
- d : formats decimal integer [byte, short, int, long]
- f : formats floating-point number [float, double]
- c : formats character Capital C will uppercase the letter
- s : formats String Capital S will uppercase all the letters in the string
- n : adds a new line character
- t : formats date/time values.
Flags
The [flags] define standard ways to modify the output.
- - : left-justify ( default is to right-justify )
- + : output a plus ( + ) or minus ( - ) sign for a numerical value
- 0 : forces numerical values to be zero-padded ( default is blank padding )
Width
The [width] specifies the field width for outputting the argument. It represents the minimum number of characters to be written to the output.
Precision
The [.precision] specifies the number of digits of precision or the length of a substring to extract from a String. Numbers are rounded to the specified precision.
Solution without using printf:
import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x = sc.nextInt(); String newX = ""; if(x>=0 && x<=9) { newX = "00"; } else if(x>=10 && x<=99) { newX = "0"; } else { newX = ""; } int ct = s1.length(); int space = 15 - ct; String bspc = ""; for(int j=0; j<=space-1; j++) { bspc = bspc +" "; } System.out.println(s1 + bspc + newX+x); } System.out.println("================================"); } }
Comments
Post a Comment