The Artima Developer Community
Sponsored Link

Java Answers Forum
Comparing Arrays

1 reply on 1 page. Most recent reply: Nov 6, 2005 10:32 AM by cristi pela

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jimbo fisher

Posts: 4
Nickname: r0punk
Registered: Oct, 2005

Comparing Arrays Posted: Oct 28, 2005 1:26 AM
Reply to this message Reply
Advertisement
I have written this code and am seeking help to determine how I would now compare the two arrays that I have scanned. In my assignment I am supposed to print them in the format I have and then a line underneath

Correct Answers: 1 2 4 5 6
Your Answers: 1 3 5 5 6
Questions missed: - * * - -

a dash for correct and a * for incorrect.. but i'm not really sure how to compare the two arrays >< any help would be greatly appreciated.
import java.util.Scanner;
 
class Project7A2
{
	public static void main(String[] args)
	{
		//declare all variables/arrays
		int questions, count, count2, count3, count4;
		int[] xxx, rightAnswers, gradedAnswers;	
		
		//set up the scanner and get the number of questions on the test-which will be size of all arrays
		Scanner scan = new Scanner(System.in);
		System.out.print("How many questions were on the quiz? ");
		questions = scan.nextInt();
		
		//set all the arrays to size of questions that was just input by user
		xxx = new int[questions];
		rightAnswers = new int[questions];
		gradedAnswers = new int[questions];		
		
		//read in the array of correct answers(answer key)
		System.out.print("\nEnter the correct answers for "+questions+" questions. (Answer key)\n");
		for(count = 0; count < xxx.length; count++)
		{
			rightAnswers[count] = scan.nextInt();
		}
		//read in the array of numbers to be checked against the key
		System.out.print("\nEnter the answers for the quiz to be graded\n");
		for(count2 = 0; count2 < xxx.length; count2++)
		{
			gradedAnswers[count2] = scan.nextInt();
		}
			
		//simple formatted printing as shown on assignment sheet
		System.out.println("\n     GRADE REPORT     ");
		System.out.println("----------------------");
		//print the correct answers as entered		
		System.out.print("Correct Answers: ");
		for(count3 = 0; count3 < rightAnswers.length; count3++)
		{
			System.out.print(" "+rightAnswers[count3]);
		}
		//print the graded answers as entered
		System.out.print("\nYour Answers:    ");
		for(count4 = 0; count4 < gradedAnswers.length; count4++)
		{
			System.out.print(" "+gradedAnswers[count4]);
		}	
	
	}
}


cristi pela

Posts: 1
Nickname: krissti
Registered: Nov, 2005

Re: Comparing Arrays Posted: Nov 6, 2005 10:32 AM
Reply to this message Reply
hy!
I know that's too late but I think that my solution might help
you sometimes:

public static void main(String []arg){
//here are some vectors;
Vector vectorQuestions=new Vector(),vectorAnswers=new Vector();
int questions, count, count2, count3, count4,report;
int[] xxx, rightAnswers, gradedAnswers;

//set up the scanner and get the number of questions on the test-which will be size of all arrays
Scanner scan = new Scanner(System.in);
System.out.print("How many questions were on the quiz? ");
questions = scan.nextInt();

//set all the arrays to size of questions that was just input by user
xxx = new int[questions];
rightAnswers = new int[questions];
gradedAnswers = new int[questions];

//read in the array of correct answers(answer key)
System.out.print("\nEnter the correct answers for "+questions+" questions. (Answer key)\n");
for(count = 0; count < xxx.length; count++)
{
rightAnswers[count] = scan.nextInt();
}
//read in the array of numbers to be checked against the key
System.out.print("\nEnter the answers for the quiz to be graded\n");
for(count2 = 0; count2 < xxx.length; count2++)
{
gradedAnswers[count2] = scan.nextInt();
}

//simple formatted printing as shown on assignment sheet
System.out.println("\n GRADE REPORT ");
System.out.println("----------------------");
//print the correct answers as entered
System.out.print("Correct Answers: ");
for(count3 = 0; count3 < rightAnswers.length; count3++)
{
System.out.print(" "+rightAnswers[count3]);
vectorQuestions.addElement(new Integer(rightAnswers[count3]));
}
//print the graded answers as entered
System.out.print("\nYour Answers: ");
for(count4 = 0; count4 < gradedAnswers.length; count4++)
{
System.out.print(" "+gradedAnswers[count4]);
vectorAnswers.addElement(new Integer(gradedAnswers[count4]));
}
//AND HERE IS THE FUNNY PART:------------------------------
System.out.print("\nQuestion missed :");
for(report=0;report<xxx.length;report++){
if(vectorQuestions.elementAt(report).equals(vectorAnswers.elementAt(report))){
System.out.print(" -");
}else
System.out.print(" *");
}
//
----------------------------------------------------------------

}

Flat View: This topic has 1 reply on 1 page
Topic: Servlet Query! Previous Topic   Next Topic Topic: Arrays & Methods

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use