Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing arrays for equality

Status
Not open for further replies.

DoneHealing

Programmer
Nov 5, 2005
5
GB
Hi, I'm having some problems with a program I'm working on and was hoping that someone here might be able to help me out.

The purpose of program is to ask the user to enter number guesses into an array and then compare this with an array of random generated numbers to find out how many of the user's guesses were correct.

I really don't know how to go about comparing two arrays. I've looked up the equals() method but I don't really get how it works.
I thought maybe I'd have to make a loop to compare each element of the arrays?

This is what I have so far:
Code:
import javax.swing.JOptionPane;
import java.util.Random;

 public class NumberGuess {


	public static void main ( String args[] )
	{
	int[] num = new int[6];
    int[] randnum = new int [6];

for (int i = 0; i < num.length; i++)
{
	num[i]=Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a guess between 1 and 6 "));
	do {
		if (num[i]<1 || num[i]>6) {
			JOptionPane.showMessageDialog (null, "That is not a valid guess, please try again ");
	        num[i]=Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a number between 1 and 6 "));
			}
		}while (num[i]<1 || num[i]>6);
}

String strArray1 = "";
for (int i = 0; i < num.length; i++)
    strArray1 = strArray1 + num[i] + " ";

JOptionPane.showMessageDialog (null, "Your guesses are: " +strArray1);

Random rn = new Random();
for (int i = 0; i < randnum.length; i++)
{
    randnum[i] = rn.nextInt(6)+1;
}

String strArray = "";
for (int i = 0; i < randnum.length; i++)
    strArray = strArray + randnum[i] + " ";

JOptionPane.showMessageDialog (null, "The random numbers are: " +strArray);

	}

}

Could anyone help me out with how I could go about doing this?
 
the easiest way to compair two arrays (and im prob gonna be shot for suggesting this because its not very efficient) is to do it this way

Code:
Vector vec = new Vector;

for (int i = 0; i < array1.length; i++)
{
   for (int j = 0; j < array2.length; j++)
   {
       if ( array1[i] == array2[j] )
       {
          vec.add( array1[i] );
       }
   }
}

this goes through the first array and compairs one element at a time to every element in the second array. adds the correct number, if found, into the vector so you can see which numbers they got right.
 
Thanks for the help, but when I try to compile with that I get this error:

'(' or '[' expected
Vector vec = new Vector;
^
1 error
 
ah sorry forgot the round brackets

Vector vec = new Vector();
 
Sorry for all the hassle but I'm still coming up with some problems.
I tried compiling again and I got these errors:
----
NumberGuess.java:46: cannot find symbol
symbol : class Vector
location: class NumberGuess
Vector vec = new Vector();
^

NumberGuess.java:46: cannot find symbol
symbol : class Vector
location: class NumberGuess
Vector vec = new Vector();
----
So I added in the line
import java.util.Vector;

and got the error:
----
NumberGuess.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
----

Am I still doing something wrong?
 
never had that problem with a vector before.

comment out vec.add( array1 ); and put in:

System.out.println("number guessed is: " + array1);

see if that makes any diff.

but it could be that the vector doesnt like taking in a primitive.

you can also try to convert the int stored in the array into an Integer object.

vec.add( (Integer)array1 );

 
That seemed to work, though the output isn't what I was trying to get.

I entered the numbers:
253462

The random numbers generated were:
336131

The output was:
number guessed is: 3
number guessed is: 3
number guessed is: 3
number guessed is: 6

The results I'd be aiming to get from that would be 0 because none of the numbers there matched up.
As in:
2->3
5->3
3->6
4->1
6->3
2->1

It looks like what's happening is that it's counting numbers I guessed anywhere in the random array....
 
i thought you just wanted to see if any of the users guesses matched any in the generated array. no matter. this is what you would do.

get rid of the nested loop and just compaire each cell of both arrays at that specific point.

Code:
for (int i = 0; i < array1.length; i++)
{
   System.out.println("Entry " + i+1 + ": " + array1[i] + " -> " + array2[i]);
}

all this will do is print out the content of array1 at point i and array2 at point i.
e.g.
Entry 1: 2 -> 3
Entry 2: 5 -> 3
Entry 3: 3 -> 6
Entry 4: 4 -> 1
Entry 5: 6 -> 3
Entry 6: 2 -> 1

this will only give you a visual comarison. if you want to store the results use a vector to sore them.
 
Sorry for not explaining it more clearly in the first post. Thanks for all the help.
I'll try using this and see if I can pull it together.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top