Java System.out.printf function are often wont to print formatted output. the aim of this exercise is to check your understanding of formatting output using printf.
To get you started, some of the answer is provided for you within the editor; you want to format and print the input to finish the answer .
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.
First we have to write System.out.format statement in the place of System.out.println which is used to format format the output. Therefore our code will be look like –> System.out.format();
Next we have to use Modulus (%) format specifier in between double quote. And our code will be –> System.out.format(“% “);
Next we have to use ‘-‘ a flag for left-justified alignment after modulus which will instruct the compiler that it will be left indent, if you want to use right indent do not use “-“. So our code will be –> System.out.format(“%- “);
The length of the string must be 15 as our problem requirement. So our code will be –> System.out.format(“%-15”);
s(format values as a string) will be substituted by our String. This will mark the end of first requirement. So our code will be –> System.out.format(“%-15s”);
Now the second part of the question. again we have to use modulus( % ) format specifier and put 3d in order to format integer. As per our question requirement and integer length must be 3 . So our code will be –> System.out.format(“%-15s=”);
%n will help to go to the next line
\
import java.util.Scanner;
public class p1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1 = null;
int x = 0;
System.out.println("=========================");
for(int i=0;i<3;i++)
{
s1=sc.next();
x=sc.nextInt();
System.out.format("%-15s%03d%n", s1, x);
}
}
}
To get you started, some of the answer is provided for you within the editor; you want to format and print the input to finish the answer .
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.
First we have to write System.out.format statement in the place of System.out.println which is used to format format the output. Therefore our code will be look like –> System.out.format();
Next we have to use Modulus (%) format specifier in between double quote. And our code will be –> System.out.format(“% “);
Next we have to use ‘-‘ a flag for left-justified alignment after modulus which will instruct the compiler that it will be left indent, if you want to use right indent do not use “-“. So our code will be –> System.out.format(“%- “);
The length of the string must be 15 as our problem requirement. So our code will be –> System.out.format(“%-15”);
s(format values as a string) will be substituted by our String. This will mark the end of first requirement. So our code will be –> System.out.format(“%-15s”);
Now the second part of the question. again we have to use modulus( % ) format specifier and put 3d in order to format integer. As per our question requirement and integer length must be 3 . So our code will be –> System.out.format(“%-15s=”);
%n will help to go to the next line
\
import java.util.Scanner;
public class p1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1 = null;
int x = 0;
System.out.println("=========================");
for(int i=0;i<3;i++)
{
s1=sc.next();
x=sc.nextInt();
System.out.format("%-15s%03d%n", s1, x);
}
}
}
Comments
Post a Comment
Please do not enter any spam link