HackerRank Java- Output Formatting


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.

  1. import java.util.Scanner;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.println("================================");
  6. for (int i = 0; i < 3; i++) {
  7. String s1 = scan.next();
  8. int x = scan.nextInt();
  9. System.out.format("%-15s%03d%n", s1, x);
  10. }
  11. scan.close();
  12. System.out.println("================================");
  13. }
  14. }

download android app