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 6 of 6

Thread: Aligning Issues in the Output

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Aligning Issues in the Output

    Hi.

    I'm having trouble aligning my information. The output leaves the columns below each other and not next to each other. Here's my code (sorry, not sure how to put code tags around it):
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    public class Hurricanes2
    {
        public static void main(String[] args) throws IOException
        {
            Scanner inFile = new Scanner(new File("hurcdata2.txt"));
            int numLines = 0;
            int knots = 0;
     
            while(inFile.hasNextLine()) 
            { 
                inFile.nextLine(); 
                numLines++; 
            } 
            inFile.close(); 
            inFile = new Scanner(new File("hurcdata2.txt")); 
     
            int [] years = new int [numLines]; 
            String [] name = new String [numLines];
            int [] category = new int [numLines];
            int [] pressure = new int [numLines];
            double [] wind = new double [numLines];
            String [] month = new String [numLines];
     
            System.out.printf("%45s\n", "Hurricanes 1980 - 2006");
            System.out.println();
            System.out.println("Year     Hurricane     Catergory     Pressure (mb)     Wind Speed (mph)");
            System.out.println("=======================================================================");
     
            int i=0; 
            while(inFile.hasNext()) 
            { 
                years[i] = inFile.nextInt(); 
                month[i] = inFile.next(); 
                pressure[i] = inFile.nextInt(); 
                wind[i] = inFile.nextDouble(); 
                name[i] = inFile.next(); 
                i++; 
            } 
            inFile.close(); 
     
     
            for(int y: years)
            {
                System.out.println(y);
            }
     
            for(String n: name)
            {
                System.out.printf("%18s\n", n);
            }
     
            for(double w: wind)
            {
                System.out.printf("%25.0f\n", w);
            }
     
            for(int p: pressure)
            {
                System.out.printf("%18d\n", p);
            }
     
        }
    }
    Last edited by copeg; August 11th, 2010 at 11:25 AM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning Issues in the Output

    First, many of your print commands also issue new lines ('\n' - System.out.println will also do the same automatically but System.out.print just prints the contents), which will result in a single column output. So first you should remove those until you actually need a new line. Second, if you wish the columns to line up perfectly you may need to write some code which determines the correct spacing between each columns, outputs the value and the correct number of spaces between each output. For example, if each column has 10 spaces and you wish to print out the value '18.0', you could
    //right aligns the output text to 10 spaces
    String st = "18.0":
    for ( int i = st.length(); i < 10; i++ ){
        System.out.print(" ");
    }
    System.out.print(st);
    Last edited by copeg; August 11th, 2010 at 11:31 AM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Aligning Issues in the Output

    Another way to get your output to be in columns is to use tab (\t) characters before the String for the next column.
    The results will depend on what program is displaying the tabbed output. Not all programs honor tabbing.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Aligning Issues in the Output

    I tried both methods, but this is what ends up happening:



    The year column is displayed on one side and the hurricane name column is displayed below the year column. The spacing between them is correct, but I'm not sure how to get the columns next to each other.
    Last edited by KiwiFlan; August 12th, 2010 at 07:15 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Aligning Issues in the Output

    It would depend on what order you print them in.
    The above shows you printed all of the numbers first and then printed all of the names and then the next ones.
    Change the loop to output one item from each array on the same row/line. You'll have to use the old style for loop

  6. #6
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Aligning Issues in the Output

    i would create a class Hurricane that holds all your hurricane fields and then inside the main-method i would declare an list of arrays like ArrayList<Hurricane) hurricanes = new ArrayList<Hurricane>(); each time your read a hurricane record assign the data to the fields and then use the add-method of the ArrayList to add the record in your collection. the ArrayList has also a method size() that returns the number of elements in the list. so you don't have to iterate twice over your data file. the class Hurricane could also overwrite the toString method that returns your format string. ok, this is up to you.

    i hope, this piece of code give you an idea how you could resolve your initial question:

    		for (int i = 0; i < years.length; i++) {
    			System.out.printf("%1d %13s %5s %14d %18.0f\n", years[i], name[i],
    					category[i], pressure[i], wind[i]);
    		}

    i used a different approach to print all your elements, assuming that all years have all datas needed. if you change your data structure to an ArrayList then also the loop must change. have fun.

  7. The Following User Says Thank You to j2me64 For This Useful Post:

    KiwiFlan (August 27th, 2010)

Similar Threads

  1. file.txt and ReadLine issues
    By chansey123 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 20th, 2010, 03:46 PM
  2. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 08:49 AM
  3. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM