println whitespace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    #1

    println whitespace

    Hello,
    is there any chance to do it in a fashion way?
    Code:
    for (int i=5; i >= 0; i--) {
      System.out.prinln( space*i + "Hello world!");
    }
    I want get this:
    Code:
         Hello world!
       Hello world!
      Hello world!
     Hello world!
    Hello world!
    thanks
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Code:
    for (int i=5; i>=0; i--) {
      for (int j=i; j>=1; j--) {
        System.out.print(" ");
      }
      System.out.println( "Hello world!"); /* NOTICE you misspelled print */
    }

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      sorry, I was lloking for something general to do this and hopefully work around with the "printf"; in fact what about if I needed the opposite?
      Code:
      Hello!
       Hello!
        Hello!
         Hello!
      Thanks

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        To increase spaces:
        Code:
            public static void example(Integer num) {
                for (int i = num; i >= 0; i--) {
                    for (int j = i; j < num; j++) {
                        System.out.print(" ");
                    }
                    System.out.println("Hello world!");
                }
            }
        
            public static void main(String[] args) {
                example(5);
            }
        To reduce spaces:
        Code:
            public static void example(Integer num) {
                for (int i = num; i >= 0; i--) {
                    for (int j = i; j > 0; j--) {
                        System.out.print(" ");
                    }
                    System.out.println("Hello world!");
                }
            }
        
            public static void main(String[] args) {
                example(5);
            }
        Probably be able to make that a lot more efficient, but you get the idea.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          A shorter way:
          Code:
          for (int i=5; i>=0; i--) { 
            System.out.println("     ".substring(i) + "Hello world!"); /* NOTICE: 5 spaces */ 
          }

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            That's a great idea chaarmann!

            Comment

            • mickey0
              New Member
              • Jan 2008
              • 142

              #7
              yes, but in that way I can't see I cood get the opposite:
              Code:
              Hello
               Hello
                 Hello

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Code:
                for (int i=0; i<=5; i++) {
                  System.out.println("     ".substring(i) + "Hello world!");
                }

                Comment

                Working...