Java Output Formatting

Sort by

recency

|

1453 Discussions

|

  • + 0 comments

    As far as I know Java 7 does not receive free public updates anymore. That means the version's relevance is minimal, if not null. Hackerrank should definitely check on this.

  • + 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();
    
    
                        //this line is the key
            System.out.printf("%-15s%03d\n", s1, x);
    
        }
        System.out.println("================================");
    }
    

    }

  • + 0 comments

    java 15

    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 scan = new Scanner(System.in);
            String s = scan.next();
            int i = scan.nextInt();
            String s1 = scan.next();
            int i1 = scan.nextInt();
            String s2 = scan.next();
            int i2 = scan.nextInt();
            
            System.out.println("================================");
            System.out.printf("%-15s%03d\n",s,i);
            System.out.printf("%-15s%03d\n",s1,i1);
            System.out.printf("%-15s%03d\n",s2,i2);
            System.out.printf("================================");
            
        }
    
  • + 2 comments

    System.out.printf("%-15s%03d\n",s1,x); Here "%-15s", "%03d" everyone's doubt why we are using -15, 03 because in that question had clearly mentioned the excepted output "java" and "100" occupy 15 character spaces. '-' left-align the string in the 15-character field. %s reads the string the s1, and "%03d" %d is used as to read the integer values and occupy 3 digits.0 pad with leading zeros if the number is less than 3 digits. example : 65 is convert into 065

  • + 0 comments

    Great exercise for getting comfortable with formatted output in Java! fairplay 24 pro