Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How can I format this multi dimentional array?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default How can I format this multi dimentional array?

    This is my code
    Java Code:
    package chapter_vii;
    
    import java.util.Scanner;
    
    public class Polling_7_38 
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		
    		String[] issues = new String[5];
    		issues[0] = "The economy";
    		issues[1] = "The availability of jobs";
    		issues[2] = "National security";
    		issues[3] = "Education";
    		issues[4] = "Usefulness of questionaires";
    		
    		int[][] responses = new int[5][10];
    		int response;
    		int people;
    		
    		System.out.println("How many people will take this survey?");
    		people = input.nextInt();
    		
    		for( int i = 0 ; i < people ; i++ )
    		{
    			for( int j = 0 ; j < issues.length ; j++ )
    			{
    				System.out.println(issues[j]);
    				response = input.nextInt();
    				++responses[j][response];	
    			}
    		}
    		
    		 for (int i = 0 ; i < responses.length ; i++) 
    		 {
    			 System.out.printf("%50s", issues[i]);
    		     for ( int j = 0 ; j < responses[i].length ; j++) 
    		     {
    		         System.out.print(" " + responses[i][j]);
    		     }
    		     System.out.println("");
    		}
    	}
    }
    output

    Java Code:
    How many people will take this survey?
    1
    The economy
    1
    The availability of jobs
    1
    National security
    1
    1Education
    
    Usefulness of questionaires
    1
                                           The economy 0 1 0 0 0 0 0 0 0 0
                              The availability of jobs 0 1 0 0 0 0 0 0 0 0
                                     National security 0 1 0 0 0 0 0 0 0 0
                                             Education 0 1 0 0 0 0 0 0 0 0
                           Usefulness of questionaires 0 1 0 0 0 0 0 0 0 0
    This is the best I can do, but I want the question on the left and the responses lined up with themselves on the right... but this is a problem because their position is determined by the question, so tabs after the question will give different lengths. I cant format the inside loop because that will be applied to each element

    How can I format this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I format this multi dimentional array?

    You could always use a negative format String:

    System.out.printf("%-50s", issues[i]);

    Although consider using a number smaller than 50.
    EscSequenceAlpha likes this.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How can I format this multi dimentional array?

    thanks freind

    didnt know you could do that

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I format this multi dimentional array?

    You're quite welcome!

Similar Threads

  1. Multi Array dec
    By maya700 in forum New To Java
    Replies: 5
    Last Post: 07-23-2011, 11:29 AM
  2. Replies: 2
    Last Post: 04-01-2011, 03:04 PM
  3. multi or two dimensional array
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-12-2010, 06:52 PM
  4. adding elemnts of two dimentional array into vector
    By sara12345 in forum New To Java
    Replies: 10
    Last Post: 12-31-2009, 11:07 AM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •