Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Output alignment help

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output alignment help

    Hi there, I need some help with my output alignment which i cannot get my head around.

    My code is the following

    import java.io.*;
    import java.util.*;
     
    public class Main{
      public static void main(String args[])
      {
     while(true)
     {
      System.out.print("#Enter word: ");
      String word = BIO.getString();
      if(word.equals("END") )
      break;
     
     
      char[] content = word.toCharArray();
     
      java.util.Arrays.sort(content);
      String sorted = new String(content);
     
      if (sorted.equals(word) )
     
      System.out.printf(word + "Letters in ascending order \n");
      else
     
      System.out.printf(word + "Letters not in ascending order \n");
      }
    }
    }

    Which using would give an output like this -

    almost Letters in ascending order
    effort Letters in ascending order
    end Letters not in ascending order
    knotty Letters in ascending order
    known Letters not in ascending order

    However i want the output to look like this -

    almost letters in ascending order
    effort letters in ascending order
    end letters not in ascending order
    knotty letters in ascending order
    known letters not in ascending order

    It does not show it properly on forums so here is a print screen.

    blah.jpg

    The top 2 lines are my current output, the bottom 2 lines are how i want it to look.
    As you can see the alingment is all formatted nicely, so it is printed out neatly.

    Can someone show me how to do this please

    Thanks


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Output alignment help

    Have a look at printf and Formatter.

    Reading: Formatter (Java 2 Platform SE 5.0)

    Chris

Similar Threads

  1. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  2. Output Alignment
    By Fahmi in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 22nd, 2010, 04:14 PM
  3. Output Alignment
    By xdrechsler in forum Loops & Control Statements
    Replies: 1
    Last Post: August 18th, 2010, 10:26 AM
  4. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM