Output Formatting Solution

This Java program is designed to take user input and format it in a specific way. The user is asked to input a string and an integer three times, and each time, the program formats and prints the input in a specific layout.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Output Formatting Solution HackerRank solution using CodeRankGPT:

The solution uses a for loop to iterate three times, each time taking a string and an integer as input from the user. It then uses the printf function to format the string to be left-justified and 15 characters wide, and the integer to be a three-digit number, padding with zeros if necessary. The formatted string and integer are then printed on the console.


package com.javaaid.hackerrank.solutions.languages.java.introduction;

import java.util.Scanner;

/**
 * 
 *
 */
public class JavaOutputFormatting {

	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("================================");
		sc.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform