Java Output Formatting

Sort by

recency

|

1473 Discussions

|

  • + 0 comments

    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("================================");
        for(int i=0;i<3;i++){
            String s=sc.next();
            int num=sc.nextInt();
            if(num<100){
                System.out.printf("%-15s",s);
                System.out.println(String.format("%03d",num));
            }else{
                System.out.printf("%-15s",s);
                System.out.println(num);
            }
        }
        System.out.println("================================");
        sc.close();
    }
    

    }

    `

  • + 0 comments
    var sc = new Scanner(System.in);
    	String t = "";
    	for(int i=0;i<3;i++) {
    		String[] a=sc.nextLine().split(" ");
    		String te = a[0];
    		int x = (int) Double.parseDouble(a[1]);
    		t += te;
    		for(int i1 = te.length(); i1 < 15;i1++){
    			t += " ";}
    		t += (x > 100)? ""+x : (x < 10)? "00"+x : "0"+x;
    		t += "\n";}
    	System.out.println("================================");
    	System.out.print(t);
    	System.out.println("================================");	
    		}
    
  • + 0 comments

    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 s = sc.next();
    > //s = sc.nextLine();
    > int n = sc.nextInt();
    > System.out.printf("%-15s%03d%n",s,n);
    > }
    > System.out.println("================================");
    > 
    > }
    > }
    > 
    > 
    > 
    > **
    https://)```
    
  • + 0 comments

    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 s = sc.next();
            //s = sc.nextLine();
            int n = sc.nextInt();
            System.out.printf("%-15s%03d%n",s,n);
            }
            System.out.println("================================");
    
        }
    

    }

  • + 1 comment

    import java.util.Scanner;

    public class Solution {

    public static void main(String[] args) {
        Object[][] dataset = new Object[3][2];
        Scanner sc = new Scanner(System.in);
        System.out.println("================================");
        for(int i = 0; i<3; i++){
            String s = sc.next();
            int n = sc.nextInt();
            Object[] pair = {s, n};
            dataset[i] = pair;
    
        }
        sc.close();
        for(int j = 0; j < 3; j++)
        {
            System.out.println(modifyString((String)dataset[j][0]) + modifyNum((int)dataset[j][1]));
        }
        System.out.println("================================");
    } 
    
    private static String modifyString(String str)
    {
        String newStr = str;
        for(int i = 0; i < (15 - str.length()); i++)
        {
            newStr += " ";
        }
        return newStr;
    }
    
    private static String modifyNum(Integer val)
    {
        int numOfNum = (int)Math.log10((double)val);
        switch(numOfNum)
        {
            case 0:
                return "00" + val.toString();
            case 1:
                return "0" + val.toString();
            case 2:
                return val.toString();
            default:
                return "000";
        }
    }
    

    }