HackerRank Java Output Formatting problem solution

In this HackerRank Java Output formatting problem in the java programming language 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.

HackerRank Java Output Formatting solution


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();
    }
}

Post a Comment

0 Comments