URGENT help needed in copying array contents
Ok so I am still doing this homework lab assignment and I was doing ok on it at first but this is starting to REALLY tick me off. So the lab that I am doing is about checking test scores by importing and processing data files and this is the main program:
//Mark Herrera
import java.util.Scanner;
import java.io.*;
import DriverExam.DriverExam;
public class Chapter7ALabDemo
{
public static void main (String [] args) throws IOException
{
File file;
Scanner readKey;
Scanner readAnswers;
String str;
int numQuestions;
DriverExam student;
int [] missedQuestions;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file that is the test key ");
str = keyboard.nextLine();
file = new File(str);
readKey = new Scanner(file);
System.out.println("Enter the name of the file with the student answers.");
str = keyboard.nextLine();
file = new File(str);
readAnswers = new Scanner(file);
System.out.println("How many test questions are there?");
numQuestions = keyboard.nextInt();
student = new DriverExam(readKey, readAnswers, numQuestions);
missedQuestions = student.questionsMissed();
System.out.println(student);
if (student.passed())
System.out.println("The student passed.");
else
System.out.println("The student did not pass.");
System.out.println("The student got " + student.totalCorrect() + " answers correct.");
System.out.println("The student got " + student.totalIncorrect() + " answers incorrect.");
System.out.println("The following questions were missed by the student: ");
student.printMissed(missedQuestions);
}
}
And now this is the class that I am making. Take a look.
Quote:
//Mark Herrera
package DriverExam;
import java.util.Scanner;
public class DriverExam {
private char[] answerKey;
private char[] studentAnswers;
public DriverExam(Scanner rKey, Scanner rAnswers, int numOfQuestions)
{
int questions = 0;
String rk;
String ra;
answerKey = new char[numOfQuestions];
studentAnswers = new char[numOfQuestions];
while (rKey.hasNext() && rAnswers.hasNext() && (questions < numOfQuestions))
{
rk = rKey.nextLine();
ra = rAnswers.nextLine();
answerKey[questions] = rk.charAt(0);
studentAnswers[questions] = ra.charAt(0);
questions++;
}
}
public int totalCorrect()
{
int correctTotal = 0;
for (int index = 0; (index < answerKey.length) && (index < studentAnswers.length); index++)
{
if (answerKey[index] == studentAnswers[index])
correctTotal++;
}
return correctTotal;
}
public int totalIncorrect()
{
int incorrectTotal = 0;
for (int index = 0; (index < answerKey.length) && (index < studentAnswers.length); index++)
{
if (answerKey[index] != studentAnswers[index])
incorrectTotal++;
}
return incorrectTotal;
}
public boolean passed()
{
if (totalCorrect() >= 0.75*answerKey.length)
return true;
else
return false;
}
public int[] questionsMissed()
{
int numQuestionsMissed = totalIncorrect();
int[] missed = new int[numQuestionsMissed];
for (int index = 0; (index < answerKey.length) && (index < studentAnswers.length); index++)
{
if (answerKey[index] != studentAnswers[index])
{
for (int number = 0; number < missed.length; number++)
{
missed[number] = index + 1;
}
System.out.println(index+1);
}
}
return missed;
}
public void printMissed(int [] questionsMissed)
{
for (int qMissed : questionsMissed)
System.out.print(qMissed);
}
}
As you can see, the red area that I've highlighted is exactly where I am having trouble. You see in that area I am supposed to create an array that shows the numbers of the questions missed. And I was trying to create it using the indexes from the student answer array field on the condition that the content of the index does not match the content of the answer key array. But this is what I got in the output.
Quote:
Enter the name of the file that is the test key
key3.txt
Enter the name of the file with the student answers.
ans3.txt
How many test questions are there?
25
1
12
13
14
25
DriverExam.DriverExam@1a758cb
The student passed.
The student got 20 answers correct.
The student got 5 answers incorrect.
The following questions were missed by the student:
2525252525
The numbers in blue are the numbers of the missed questions that are supposed to be stored in the array. But just why does the method end up only storing 25 in each element?