Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-03-2010, 04:22 PM
Junior Member
 

Join Date: Mar 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
The Lost Plot is on a distinguished road
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
Java Code
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
Java Code
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; 10-03-2010 at 12:05 PM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 10-03-2010, 12:08 PM
Junior Member
 

Join Date: Mar 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
The Lost Plot is on a distinguished road
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; 10-03-2010 at 12:10 PM.
Reply With Quote
  #3 (permalink)  
Old 10-03-2010, 02:58 PM
Faz's Avatar
Faz Faz is offline
Member
 

Join Date: Mar 2010
Posts: 94
Thanks: 5
Thanked 14 Times in 14 Posts
Faz is on a distinguished road

I'm feeling Nerdy
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)

Java Code
	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.
Java Code
 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; 10-03-2010 at 03:02 PM.
Reply With Quote
The Following User Says Thank You to Faz For This Useful Post:
The Lost Plot (12-03-2010)
  #4 (permalink)  
Old 10-03-2010, 02:59 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #5 (permalink)  
Old 10-03-2010, 03:05 PM
Faz's Avatar
Faz Faz is offline
Member
 

Join Date: Mar 2010
Posts: 94
Thanks: 5
Thanked 14 Times in 14 Posts
Faz is on a distinguished road

I'm feeling Nerdy
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.
Reply With Quote
  #6 (permalink)  
Old 12-03-2010, 04:22 PM
Junior Member
 

Join Date: Mar 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
The Lost Plot is on a distinguished road
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on java array rossfally Collections and Generics 2 05-03-2010 12:49 AM
[SOLVED] Help needed! subhvi Web Frameworks 4 18-02-2010 01:26 PM
Multi Dimensional Array help NEEDED URGENT bonjovi4u Loops & Control Statements 5 13-02-2010 04:44 AM
array program assistance needed JavaNoob82 Collections and Generics 4 14-12-2009 09:49 AM
Array program help needed SCM Loops & Control Statements 2 03-12-2009 03:28 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:00 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.