Java Output Formatting

Sort by

recency

|

1390 Discussions

|

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

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

    }

  • + 0 comments
    System.out.println("================================");
                int totalLength = 15;
                for(int i=0;i<3;i++){
                    String s1=sc.next();
                    int x=sc.nextInt();
                     //Complete this line
                    String formatnumber = String.format("%03d",x);
                    int spaceNeeded = totalLength - s1.length();
                    StringBuilder spaces = new StringBuilder();
                    for (int j = 0; j < spaceNeeded; j++) {
                        spaces.append(" ");
                    }
                    String finalString = s1+ spaces + formatnumber;
    

    undefined System.out.println(finalString); } System.out.println("=========== `=====================");

  • + 1 comment

    Scanner sc=new Scanner(System.in); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x=sc.nextInt();

                if(s1.length()<=10 && (x>=0 && x<=999)){
                    System.out.println(String.format("%-15s%03d", s1, x));
                }
            }
            System.out.println("================================");
    
    }
    
  • + 1 comment

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
          System.out.println("=".repeat(32));
    
        for(int i = 0; i <3; i+=1)
        {
        String str = sc.nextLine();
        String program = str.substring(0, str.indexOf(" "));
        String version = str.substring(str.indexOf(" "), str.length()).trim();
        version = (version.length() < 3)? "0".repeat(3-version.length())+version: version;
        System.out.println(program + " ".repeat(15-program.length())+ version);
        }
         System.out.print("=".repeat(32));
        sc.close();
    }
    

    }