Java Output Formatting

Sort by

recency

|

1398 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 s1=sc.next();
                int x=sc.nextInt();
                //Complete this line
                int count=s1.length();//4
                System.out.print(s1);
                for(int j=0;j<15-count;j++){
                    System.out.print(" ");
                }
                if(x<100 && x>=10){
                    System.out.print("0"+x);
                }
                else if(x<10){
                    System.out.print("00"+x);
                }
                else{
                    System.out.print(x);
                }
                System.out.println();
    
            }
            System.out.println("================================");
    
    }
    

    }

  • + 0 comments

    my code: 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("================================");
    
    }
    

    }

  • + 1 comment

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

    }

  • + 0 comments
    import java.util.Scanner;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Map<String, String> myHashMap = new LinkedHashMap<>();
    
            for (int i = 1; i <=3; i++){
                String s = sc.next();
                int n = sc.nextInt();
                String num = "";
                if (n < 10){
                    num = "00" + String.valueOf(n);
                }else if (n<100 && n>=10){
                    num = "0" + String.valueOf(n);
                }else{
                    num = String.valueOf(n);
                }
                myHashMap.put(s, num);
            }
            
            System.out.println("================================");
            for (Map.Entry<String, String> entry : myHashMap.entrySet()){
                System.out.printf("%-14s %s \n", entry.getKey(), entry.getValue());
            }
            System.out.println("================================");
        }
    }
    
  • + 0 comments

    public class Solution { public void display(String x,int y){ System.out.print(x);

        for(int i = x.length(); i < 15; i++){
            System.out.print(" ");
        }
    
            if(y<10){
                System.out.println("00"+y);
            }else if(y<100){
                System.out.println("0"+y);
            }else if(y<1000){
                System.out.println(y);
            }
    
    }
    
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
    
            String s1=sc.next();
            int x=sc.nextInt();
            String s2=sc.next();
            int y=sc.nextInt();
            String s3=sc.next();
            int z=sc.nextInt();
    
            Solution l = new Solution(); 
    
            System.out.println("================================");
            l.display(s1,x);
            l.display(s2,y);
            l.display(s3,z);
    
                //complete this line
    
            System.out.println("================================");
    
    }
    

    }