Connecting Tech Pros Worldwide Forums | Help | Site Map

I'm new to Java, need help with Arrays

Newbie
 
Join Date: Mar 2007
Posts: 5
#1: Mar 26 '07
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);


}
}

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Mar 26 '07

re: I'm new to Java, need help with Arrays


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

Expand|Select|Wrap|Line Numbers
  1.  if(a[i].equals(b[i]) ) ...
Reply