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

Thread: Hi, I need help outputting a 2d array

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi, I need help outputting a 2d array

    ok basically I have an arraylist with the names of soccer teams, i.e Manchester United, Man City, Arsenal, everton e.t.c and I have a 2d array with al the information inside to create a leaderboard, my problem is that my output is this:
    Team___________Played__HomeWins__HomeDraws___HomeL ossess e.t.c
    Manchester United __27______8__________3_____________1
    Man City _____27___6_______4__________3
    Everton ______27___4_______5__________6

    but I want this:
    Team___________Played__HomeWins__HomeDraws___HomeL ossess e.t.c
    Manchester United__27________8__________3____________1
    Man City __________27________6_________4____________3
    Everton ___________27________4_________5____________6

    (I used __________ because spaces didnt work)
    I cant get the results to line up under the headings correctely
    the array list has the team names in it and the 2d array has the team number for the rows i.e 1 is Man united and then the columns are "games played" "home wins" "home draws" e.t.c

    Any help would be greatly appreciated


  2. #2
    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: Hi, I need help outputting a 2d array

    I cant get the results to line up under the headings correctely
    If it is question of enough spaces to position the text in columns, see the printf() method and/or try using the \t character
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi, I need help outputting a 2d array

    OK I manged to solve this myself using the code below, the problem was that the team names can have multiple words in them based on user input making so each one would require a different number of \t's
                    int max = 0;
    		for(int i = 0; i < teamName.length; i++)
    		{
    			if(teamName[i].length() > max)
    				max = teamName[i].length();
    		}
    		System.out.print("Pos\tTeam\t\t\tP\tHW\tHD\tHL\tHF\tHA\tAW\tAD\tAL\t\tAF\tAA\tGD\tPts\n");
    		for(int i = 0; i < teamWDL.length; i++)
    		{
    			System.out.print((i + 1) + "\t" + teamName[i]);
    			if(i > 0)
    			{
    					for(int s = 0; s < max - teamName[i].length(); s++)
    						System.out.print(" ");
    			}
    			for(int j = 0; j < leaderBoard[0].length; j++)
    				System.out.print("\t" + leaderBoard[i][j]);
    			System.out.print("\t" + teamWDL[i]);
    			System.out.print("\n");
    		}

  4. #4
    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: Hi, I need help outputting a 2d array

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    I don't know how to find out the spacing generated by a tab character so that you could use the length of a String to compute the number of tab characters needed.

    Have you tried using spaces to position the columns?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi, I need help outputting a 2d array

    Sorry, yes I managed to solve the problem using spaces instead of tabs. Thanks for your quick responses btw, should I delete this thread because the problem is solved or should I leave it for others to see?

Similar Threads

  1. Reading File and Outputting Data
    By ChrisMessersmith in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 13th, 2011, 01:40 PM
  2. Help with code outputting wrong info
    By colorlesscliche in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 01:39 AM
  3. Need help outputting the array to a .csv
    By arpanetguru in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 23rd, 2010, 05:27 PM
  4. Need help outputting sub-list of an ArrayList
    By Allusive in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2010, 09:20 AM