Java Output Formatting

  • + 2 comments

    Here we used printf to repeat the values of 0 and space. '-%15s' means that the length of string will be 15 and spaces will be repeatedly printed on right side of string. And '%03d' means length of string will be 3 and '0' will be repeatedly printed on left side of string.

    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",s1);
    				System.out.printf("%03d",x);
    				System.out.println();
    					  }
    				System.out.println("================================");
    
        }
    }