Results 1 to 1 of 1
  1. #1
    Terminus_Est is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Exclamation 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.

    //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.

    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?
    Last edited by Terminus_Est; 03-27-2012 at 07:21 AM.

Similar Threads

  1. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  2. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 06:36 PM
  3. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 08:03 AM
  4. Help needed Clearing the contents of a File
    By sandeepsai17 in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 03:31 PM
  5. copying contents from word document to JTextPane
    By success21061985 in forum Advanced Java
    Replies: 1
    Last Post: 11-05-2008, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •