Quote:
Originally Posted by ray2007colon
I need to now how to compare the elements of two arrays. Here is my program. Now I need to compare each element. So I can print the arrays and the elements that are not the same.
import java.util.*;
public class Grades
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many question are in the quiz?");
int num = keyboard.nextInt();
keyboard.nextLine();
String[] Grades = new String [num];
String[] Student = new String [num];
for (int count = 0; count < Grades.length; count++)
{
System.out.println("Please enter key of the quiz(please upper case) " + count );
Grades[count] = keyboard.nextLine();
}
for (int st = 0; st < Student.length; st++)
{
System.out.println("Please enter the student answers " + st );
Student[st] = keyboard.nextLine();
}
System.out.println("quiz key\tstudent answer");
for (int a=0; a<Student.length;++a){
System.out.println(" " + Grades [a]+ "\t\t\t" + Student [a]);
}
System.exit(0);
}
}
if you have two arrays a and b of reference types and you want to compare the elements at position i of both arrays then you can use
- if(a[i].equals(b[i]) ) ...