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 FormatEvery 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 FormatIn 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 Inputjava 100cpp 65python 50Sample Output================================java 100 cpp 065 python 050
Question
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 FormatEvery 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 FormatIn 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 Inputjava 100cpp 65python 50Sample Output================================java 100 cpp 065 python 050
Solution
Here is a step-by-step solution in Java:
- First, import the Scanner class from the java.util package. This class is used to get input from the user.
import java.util.Scanner;
- Create a new Scanner object to read the input from the user.
Scanner sc=new Scanner(System.in);
- Create a loop to read each line of input. The loop should continue until there is no more input.
while(sc.hasNext())
- Inside the loop, read the string and the integer from the input.
String s1=sc.next();
int x=sc.nextInt();
- Still inside the loop, use the printf function to print the string and the integer in the required format. The "-" in "%-15s" means left-justified, "15" is the width of the string, "03" is the width of the integer and it will be padded with leading zeroes if it has less than 3 digits.
System.out.printf("%-15s%03d%n", s1, x);
- Close the scanner object after the loop.
sc.close();
Here is the complete code:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
while(sc.hasNext()){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s%03d%n", s1, x);
}
System.out.println("================================");
sc.close();
}
}
This code will print each string left-justified with exactly 15 characters and each integer with exactly 3 digits, padding with leading zeroes if necessary.
Similar Questions
Input FormatEvery 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 FormatIn 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 Inputjava 100cpp 65python 50Sample Output================================java 100 cpp 065 python 050 ================================ExplanationEach 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.
In this challenge, you must read an integer, a double, and a String from stdin, then print the values according to the instructions in the Output Format section below. To make the problem a little easier, a portion of the code is provided for you in the editor.Note: We recommend completing Java Stdin and Stdout I before attempting this challenge.Input FormatThere are three lines of input:The first line contains an integer.The second line contains a double.The third line contains a String.Output FormatThere are three lines of output:On the first line, print String: followed by the unaltered String read from stdin.On the second line, print Double: followed by the unaltered double read from stdin.On the third line, print Int: followed by the unaltered integer read from stdin.To make the problem easier, a portion of the code is already provided in the editor.Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).Sample Input423.1415Welcome to HackerRank's Java tutorials!Sample OutputString: Welcome to HackerRank's Java tutorials!Double: 3.1415Int: 42
Write a code to get the input in the given format and print the output in the given formatInput Description:To take an integer valueOutput Description:Print the integer valueSample Input :2Sample Output :2
What is the output of the following Java code snippet? (\n in output is to be assumed to be the new line character) public class Main { public static void main(String[] args) { System.out.print("Hello "); System.out.println("World"); System.out.printf("Number: %d", 10); }}
Write a program that takes a number 𝑁N as the input, and prints it to the output.Input FormatThe only line of input contains a single integer.Output FormatOutput the answer in a single line.Constraints0≤𝑁≤1050≤N≤10 5 Sample 1:InputOutput123123
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.