In this HackerRank Java Output formatting problem in the java programming language in Every line of input will contain a String followed by an integer. Each String will have a maximum of 10 alphabetic characters, and each integer will be in the inclusive range from 0 to 999.
In each line of output there should be two columns:
- The first column contains the String and is left-justified using exactly 15 characters.
- The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.
HackerRank Java Output Formatting 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(); System.out.printf("%-15s%03d%n", s1, x); } System.out.println("================================"); } }
Second solution in java8 programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); System.out.println("================================"); String [] splitLine; String s; int i; while(sc.hasNextLine()){ splitLine = sc.nextLine().split(" "); s = splitLine[0]; i = Integer.parseInt(splitLine[1]); System.out.printf("%-15s%03d\n",s,i); } System.out.println("================================"); sc.close(); } }
0 Comments