Java Output Formatting

Sort by

recency

|

1471 Discussions

|

  • + 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";
        }
    }
    

    }

  • + 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 s1=sc.next();
            int y=sc.nextInt();
    
        while (s1.length()<15) {s1=s1+" ";}
    
        if (y<10) {System.out.println(s1+"00"+y);}
        else  if (y<100) {System.out.println(s1+"0"+y);}
    else {System.out.println(s1+y);}
        }
        System.out.println("================================");
    

    } }

  • + 0 comments

    need to start learning format :(

    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();
    
            while (s1.length()<15) {s1=s1+" ";}
    
            if (x<10) {System.out.println(s1+"00"+x);}
            else  if (x<100) {System.out.println(s1+"0"+x);}
        else {System.out.println(s1+x);}
            }
            System.out.println("================================");
    
    }
    

    }