Go Back   Java Programming Forums > Java Standard Edition Programming Help > Loops & Control Statements


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2010, 04:12 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 help needed

ok so ive to hand this question in by the end of today 19:00

Here's the Q.Declare an array of 50 student numbers and fill it with integers between 1 and 1000.
Output all elements to the screen. Check if there are duplicates and if so output the index at which these duplicates occur. e.g. Student[21] = 444 Student[33] = 444

I know my problem is in the 3rd nested loop with the intiger i,

Java Code
class WS5bQ2
{
	public static void main(String args[])
	{
		int grade[][] = new int[1][50];
		for(int row=0; row<grade.length; row++)//loop to assingn values to the array
		{
			for(int col=0; col<50; col++)//controls the col
			{
			grade[row][col] = (int)(Math.random()*10000000%1000+1);
			}
		}
		for(int row=0; row<grade.length; row++)//output array to screen
		{
			for(int col=0; col<50; col++)
			{
			System.out.print(grade[row][col]+"  ");
			}
		}
		System.out.println("");
		for(int row =0; row<grade.length; row++)//nested loop to check for matching grades
		{
			for(int col = 1; col <grade.length; col++)
			{
				int i = 0;
				if(grade[row][i] == grade[row][col])
				{
					System.out.println("Stundent "+i+  "= "+grade[row][i]+"   Stundent "+col+"= "+grade[row][col]);
				}
				i++;

			}//close second loop
		}//close 1st loop
	}//close main
}//close class
ive laso tried the 3rd nested loop this way

Java Code
		for(int i =0; i<=49; i++)//nested loop to check for matching grades
		{
			for(int col = 1; col <grade.length; col++)
			{
				if(grade[0][i] == grade[0][col])
				{
					System.out.println("Stundent "+i+  "= "+grade[0][i]+"   Stundent "+col+"= "+grade[0][col]);
				}
			}//close second loop
		}//close 1st loop



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 12-03-2010, 04:32 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: help needed

you're checking the lengths incorrectly. The .length field only returns the length for that dimension.

ex.

Java Code
int[][] myMatrix = new int[5][10];
System.out.println("there are " + myMatrix.length + " rows in the matrix.");
System.out.println("there are " + myMatrix[0].length + " elements in row 0.");
I also believe that there's no need for the multi-dimension arrays to be "rectangular".
Java Code
int[][] myMatrix = new int[5][];
myMatrix[0] = new int[6];
myMatrix[1] = new int[7];
System.out.println("their are " + myMatrix.length + " rows."); // 5
System.out.println("there are " + myMatrix[0].length + " elements in row 0."); // 6
System.out.println("there are " + myMatrix[1].length + " elements in row 0."); // 7
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 12-03-2010, 04:56 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: help needed

i dont really know what you mean rectangle,
ive got the array to output the matches but it always matches itself with itself aswell, can anyone show me how to reslove this problem
Java Code
		for(int i= 1; i<=49; i++)// checking for duplicate entry's in the array and out index of duplicate
		{
			for(int col = 0; col <49;col++)
			{
				if(grade[0][col] == grade[0][i])
				{
					System.out.println("Stundent "+i+  "= "+grade[0][i]+"   Stundent "+col+"= "+grade[0][col]);
				}
			}//close second loop
		}

Last edited by The Lost Plot; 12-03-2010 at 05:41 PM.
Reply With Quote
  #4 (permalink)  
Old 12-03-2010, 08:13 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: help needed

A rectangular 2 dimensional array simply means that every row has the same number of elements. ex:

1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
1 2 3 4 5 6

this is rectangular because every row has 6 elements.

1 2 3
1 2 4 5 6 7
1 2 3
1 2 3 4
1 2 3 4 5

this isn't rectangular because there are rows with different number of elements. Anyways, the point I was trying to point out was that you were trying to get the length of the wrong thing.

Java Code
		for(int row =0; row<grade.length; row++)//nested loop to check for matching grades
		{
			for(int col = 1; col <grade[row].length; col++) // this line is different
			{
				int i = 0;
				if(grade[row][i] == grade[row][col])
				{
					System.out.println("Stundent "+i+  "= "+grade[row][i]+"   Stundent "+col+"= "+grade[row][col]);
				}
				i++;

			}//close second loop
		}//close 1st loop
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #5 (permalink)  
Old 16-03-2010, 07:55 AM
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: help needed

Firstly you don't don't have a 3rd nested loop it's a for statement inside nested loop, just so you realise .

Secondly you just need a statement to prevent it from running when it's comparing the same one. You can make another if statement outside it but I would change your current one to:

Java Code
	if(grade[row][i] == grade[row][col] && i != col)
Have you gone through this sort of stuff yet?
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 with netbeans jalkin Java IDEs 1 05-05-2010 02:08 AM
[SOLVED] Help needed! subhvi Web Frameworks 4 18-02-2010 01:26 PM
[SOLVED] A little help needed.. JavaStudent87 What's Wrong With My Code? 0 22-01-2010 10:54 PM
ebooks needed programmer2009 The Cafe 2 15-12-2009 04:33 AM
Project help needed crazydeo Collections and Generics 4 22-05-2008 03:59 PM


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:03 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.