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: Array help needed

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array help needed

    Q.You should use a two dimensional array of 5 columns to represent each day and 4 rows to represent each shop.
    Fill in the number of customers every shop had over the week using random numbers between 100 and 500.
    The totals should be calculated and displayed beside each row when you output the array.

    Ive no problem doing this part of the code but i cant figure out part b
    class WS5Q1
    {
    	public static void main(String[] args)
    	{
    		int shoptotal [] [] = new int [4] [5];
    		String[] shopname = new String[4];
    		int total = 0;
    		shopname[0] = "Grafton street ";
    		shopname[1] = "O Connel street";
    		shopname[2] = "Blanchardstown ";
    		shopname[3] = "Liffey Valley  ";
    		for(int shop=0; shop<4; shop++)//controls the rows
    		{
    			for(int col=0; col<5; col++)//controls the colum's
    			{
    			shoptotal[shop] [col] = (int)(Math.random()*10000000%500+1); //random number generator
    			}
    		}
    		System.out.println("	                         Mon    Tue     Wed     Thu     Fri     Total");
    		for(int shop=0;shop<shoptotal.length;shop++)
    		{
    			System.out.print("Shop:" + shop +":"+ shopname[shop]+"           ");
    			for(int col=0; col<shoptotal[shop].length; col++)//loop to output results and keep track of total for each week
    			{
    				total = total + shoptotal[shop][col];
    				System.out.print(shoptotal[shop][col] + "	");
    			}
    				System.out.println(+total);
    				total = 0;
    		}
    		System.out.println();
    	}//end main
    }//end class

    Problem (b)

    Amend the above program such that the shop with the largest number of customers on a particular day is noted. For example, from the above sample data, the output would be:
    Shop 1 – O’Connell Street had the most customers on Tuesday at 499 people
    Here's my attempt
    class WS5Q2
    {
    	public static void main(String[] args)
    	{
    		int shoptotal [] [] = new int [4] [5];
    		String[] shopname = new String[4];
    		int total = 0, largest1 = 0, largest2 = 0, largest3 = 0, largest4 = 0, largest5 = 0;
    		shopname[0] = "Grafton street ";
    		shopname[1] = "O Connel street";
    		shopname[2] = "Blanchardstown ";
    		shopname[3] = "Liffey Valley  ";
    		for(int shop=0; shop<4; shop++)//controls the rows
    		{
    			for(int col=0; col<5; col++)//controls the colum's
    			{
    			shoptotal[shop] [col] = (int)(Math.random()*10000000%500+1); //random number generator
    			}
    		}
    		System.out.println("	                         Mon    Tue     Wed     Thu     Fri     Total");
    		for(int shop=0;shop<shoptotal.length;shop++)
    		{
    			System.out.print("Shop:" + shop +":"+ shopname[shop]+"           ");
    			for(int col=0; col<shoptotal[shop].length; col++)//loop to output results and keep track of total for each week
    			{
    				total = total + shoptotal[shop][col];
    				System.out.print(shoptotal[shop][col] + "	");
    			}
    				System.out.println(+total);
    				total = 0;
    		}
    		System.out.println();
    			for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][0]>largest1)
    					{
    					largest1 = shoptotal[shop][0];
    					}
    			}
    			for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][1]>largest2)
    					{
    					largest2 = shoptotal[shop][1];
    					}
    			}
    			for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][2]>largest3)
    					{
    					largest3 = shoptotal[shop][2];
    					}
    			}
    			for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][3]>largest4)
    					{
    					largest4 = shoptotal[shop][3];
    					}
    			}
    			for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][4]>largest5)
    					{
    					largest5 = shoptotal[shop][4];
    					}
    			}
    			System.out.println("had the most visits on Monday with " +largest1+ " customers");
    			System.out.println("had the most visits on Tuesday with " +largest2+ " customers");
    			System.out.println("had the most visits on Wednesday with " +largest3+ " customers");
    			System.out.println("had the most visits on Thursday with " +largest4+ " customers");
    			System.out.println("had the most visits on Friday with " +largest5+ " customers");
    	}//end main
    }//end class
    im not sure if im even going about this the right way
    any help would be greately appricated
    Last edited by The Lost Plot; March 10th, 2010 at 08:05 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array help needed

    Shop 1 – O’Connell Street had the most customers on Tuesday at 499 people

    ok ive got the program to find the most visits per shop per day but im having trouble outputting the part of the line highlighted in red

    i think i could use an if/ else if statements but this will make my code very bluky if you know what i mean is there a simpler way to do this
    Last edited by The Lost Plot; March 10th, 2010 at 08:10 AM.

  3. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array help needed

    OK the just looking at the code I think you just need to add a few index variables. It's definatly not the most elegant solution as I would have the print statement in the loop(this would need an array with day strings though so up to you). SO declare index(as ints and initialise them in the loop)

    	for(int shop = 0; shop < shoptotal.length;shop++)
    			{
    					if(shoptotal[shop][0]>largest1)
    					{
    					largest1 = shoptotal[shop][0];
                                                                                                               index1 = shop
    					}
    			}

    The rest should be easy

    EDIT: On second thought you could drop the largest variables and just use the index.
     on Monday with " + shoptotal[index1][0] + " Customers

    Once again it's not the most elegant solution as I would use a few nested loops but I don't know if your interested in completely changing your code.
    Last edited by Faz; March 10th, 2010 at 11:02 AM.

  4. The Following User Says Thank You to Faz For This Useful Post:

    The Lost Plot (March 12th, 2010)

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array help needed

    If you're just printing out to the console screen, I would strongly recommend against trying to get different colors. The only other color other than the standard black is the red (and actually, this only hold some times) is to use the System.err stream. However, this stream should only be used for Exception handling, and not for standard output.

    If you want different colors, create a Swing application and use a JTextPane to format you colors accordingly. Note that you will need some way to distinguish between what should be red and what should be black. The easiest way usually is to keep the two in separate strings. Using if/else statements is the second method.

  6. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Array help needed

    Quote Originally Posted by helloworld922 View Post
    If you're just printing out to the console screen, I would strongly recommend against trying to get different colors. The only other color other than the standard black is the red (and actually, this only hold some times) is to use the System.err stream. However, this stream should only be used for Exception handling, and not for standard output.

    If you want different colors, create a Swing application and use a JTextPane to format you colors accordingly. Note that you will need some way to distinguish between what should be red and what should be black. The easiest way usually is to keep the two in separate strings. Using if/else statements is the second method.
    I don't think he wanted to print it in red, I think he just meant that he didn't know how to print it at all. The highlighting was done to show us what he couldn't do.

    I think.

  7. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array help needed

    yeah the part in red was to show the bit of the output i was stuck on,i ended up declaring a second arary for the days and finding the highest total of visits for the week and not per day.

    Cheers

Similar Threads

  1. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 08:49 PM
  2. [SOLVED] Help needed!
    By subhvi in forum Web Frameworks
    Replies: 4
    Last Post: February 18th, 2010, 09:26 AM
  3. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  4. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 05:49 AM
  5. Array program help needed
    By SCM in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2009, 11:28 PM