Java Output Formatting

Sort by

recency

|

1446 Discussions

|

  • + 0 comments

    It really helps you understand how to control output formatting, which is super useful when displaying clean and readable data—especially in tables or logs. www Raja567 com

  • + 0 comments

    For Java 7 and above:

    for(int i=0;i<3;i++) { String s1=sc.next(); int x=sc.nextInt(); System.out.printf("%-15s%03d%n", s1, x); }

    For Java 11 and above:

    for(int i=0;i<3;i++) { String s1=sc.next(); int x=sc.nextInt(); int l=15-s1.length(); System.out.printf("%s%s%03d\n", s1," ".repeat(l), x); }

    For Java 7 and above:

  • + 0 comments

    For Java15

    import java.util.Scanner;
    
    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();
                int n = sc.nextInt();
                int l = 15-s.length();
                
                System.out.printf("%s%s%03d\n", s, " ".repeat(l), n);
            }
            System.out.println("================================");
        }
    }
    
  • + 0 comments

    This is a great exercise for learning how to control output formatting with System.out.printf in Java. Very helpful for beginners! Raja 567

  • + 0 comments

    This was a great way to practice formatted output in Java! I liked how the exercise focused on both left-justifying strings and zero-padding integers—super useful skills for beginners to learn early on. Betbricks7 ID