Results 1 to 1 of 1
- 03-27-2012, 06:16 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
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.
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.//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);
}
}
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?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:
2525252525Last edited by Terminus_Est; 03-27-2012 at 06:21 AM.
Similar Threads
-
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
Trouble copying an array
By xXRedneckXx in forum New To JavaReplies: 10Last Post: 02-05-2011, 05:36 PM -
Copying ArrayList into an Array
By Manfizy in forum New To JavaReplies: 6Last Post: 07-16-2009, 07:03 AM -
Help needed Clearing the contents of a File
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 07-02-2009, 02:31 PM -
copying contents from word document to JTextPane
By success21061985 in forum Advanced JavaReplies: 1Last Post: 11-05-2008, 06:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks