Brass P.
0
Q:

formatted output in java

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();
                //Complete this line                
                System.out.printf("%-15s%03d%n",s1,x);
            }
            System.out.println("================================");

    }
}
0
// A Java program to demonstrate working of printf() in Java 
class JavaFormatter1 
{ 
  public static void main(String args[]) 
  { 
    int x = 100; 
    System.out.printf("Printing simple integer: x = %d\n", x); 
  
    // this will print it upto 2 decimal places 
    System.out.printf("Formatted with precison: PI = %.2f\n", Math.PI); 
  
    float n = 5.2f; 
  
    // automatically appends zero to the rightmost part of decimal 
    System.out.printf("Formatted to specific width: n = %.4f\n", n); 
  
    n = 2324435.3f; 
  
    // here number is formatted from right margin and occupies a 
    // width of 20 characters 
    System.out.printf("Formatted to right margin: n = %20.4f\n", n); 
  } 
} 
3

New to Communities?

Join the community