Java Output Formatting

Sort by

recency

|

1408 Discussions

|

  • + 0 comments

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

    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.
         */
        List<Map<String, Integer>> lines = getLines(args);
        printLines(lines);
    }
    
    public static List<Map<String, Integer>> getLines(String[] args) {
        List<Map<String, Integer>> list = new ArrayList<>();
    
        Scanner scanner = new Scanner(System.in);
    
        while (scanner.hasNext()) {
            String name = scanner.next();
            int value = scanner.nextInt();
    
            Map<String, Integer> result = new HashMap<>();
            result.put(name, value);
            list.add(result);
        }
    
        scanner.close();
    
        return list;
    }
    
    public static void printLines(List<Map<String, Integer>> list) {
        System.out.println("================================");
        for (int i = 0;i<list.size();i++) {
            Map<String, Integer> map = list.get(i);
            map.forEach((k, v) -> { System.out.printf("%-15s%03d\n", k, v); });
    
        }
    
        System.out.println("================================");
    
    }
    

    }

  • + 0 comments

    Getting expected Output still not passing the scenario

    ` 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<100){
                    System.out.println(s1+"\t\t"+"0"+x);
                }else{
                    System.out.println(s1+"\t\t"+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;

    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 spaceNeeded= 10-s1.length();
                String space="";
                for(int j=0;j<spaceNeeded+5;j++){
                   space=space.concat(" ");
                }
    
                String intNum=Integer.toString(x);
                if(intNum.length()==3) intNum=intNum;
                else if (intNum.length()==2) intNum="0"+intNum;
                else if (intNum.length()==1) intNum="00"+intNum;
                System.out.println(s1+space+intNum);
            }
            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.format("%-15s",s1);
                System.out.format("%03d%n",x);   
            }
            System.out.println("================================");
    
    }
    

    }