2

I am working on an assignment for class and I'm stuck at the very beginning. I'm not sure how to go about the user input, I'll elaborate after I tell you what the assignment is....

The first input will be the answer key to a quiz of ten T or F answers. It then takes user input for Students first and last name, ID #, and then answers to a "quiz" of T/F, the user enters as many students as they want and then "ZZZZ" to terminate. All of the user input is entered in one entry and that's where I'm having issues.

An example input for the program:

1 T T T T T F T T F F
Bobb, Bill 123456789  T T T T T F T T F F
Lou, Mary  974387643  F T T T T F T T F F
Bobb, Sam  213458679  F T F T f t 6 T F f
Bobb, Joe  315274986  t t t t t f t t f f
ZZZZ

which will produce the output: Results for quiz 1:

123-45-6789  Bill Bobb 10
974-38-7643  Mary  Lou 9
213-45-8679  Sam  Bobb 5
315-27-4986  Joe  Bobb 10

The average score is 8.5

We have to use BufferedReader and all of the input is entered all at once. The issue I'm having is I do not know how to go about the input. At first I figured I would split the input by newline and create an array where each index is the newline, but what I have now only prints "ZZZZ" and I can't figure out why? I also don't know how to go about comparing the first index (answer key) with all the students answers. Once I split the input by newlines can I then split each index in that array by space? Any help is greatly appreciated! Please keep in mind I'm very new to Java.

What I have so far (I know its not much but I just got stuck right up front)....

public class CST200_Lab4 {

public static void main(String[] args) throws IOException {

    String inputValue = " ";
    String inputArr[] = new String[13];
    String answerKey = null;
    String numStudents[];

    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);


    while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
        inputValue = BR.readLine();
        inputArr = inputValue.split("\\r+");
        answerKey = inputArr[0];        
    }
    System.out.println(answerKey);      
}

}

2 Answers 2

2

Use this code inside main()

    String inputValue = " ";
    String inputArr[] = new String[13];
    String answerKey = null;
    String numStudents[];

    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);

    try {
        inputValue = BR.readLine();
        String answers[] = inputValue.split(" "); 
        int count = 0;
        System.out.println();
        while((inputValue = BR.readLine()) != null) {
            if (inputValue.equalsIgnoreCase("ZZZZ"))
                break;
            inputArr = inputValue.split("\\s+");
            System.out.print(inputArr[2] + " ");
            System.out.print(inputArr[1] + " ");
            System.out.print(inputArr[0].split(",")[0] + " ");
            count = 0;
            for(int i = 0; i <10; i++){
                if(inputArr[i+3].equalsIgnoreCase(answers[i+1]))
                    count ++;
            }
            System.out.print(count);
            System.out.println();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }      

}

I have left the average part for you to calculate. Mention not.

Sign up to request clarification or add additional context in comments.

4 Comments

That's giving an index out of bounds exception....Also, my professor will literally mark off points if we use Break in our while loops, no joke.
This works fine !! and come on man you can just modify the if logic to eliminate break. WHich line are oyu getting the exception ?
Run the above code using the exact same input that you posted in your question. It runs for me that way. You must be altering the format of input in some way i guess which is causing you the exception. Also I would say it is better to include the input in a file and then read from it instead of reading it from console
I think it might be the print line after count is initialized, I didn't hit enter I entered more input. I'll try agin in the morning. Thank you for your help guys
1

I just typed this code here, so there may be typos and you should validate it. But this is one way of doing it.

If you use an ArrayList to store student details, you don't need to know the number of students. Size of an ArrayList is dynamic.

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

import java.util.ArrayList;

public class CST200_Lab4 {

public static void main(String[] args) throws IOException {

    String inputValue = "";
    ArrayList<String[]> students = new ArrayList<String[]>()
    String[] answerdarr;

    BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));

    //first line is always the answers
    String answers = BR.readLine();
    answerArr = answers.split(" ");

    while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
        inputValue = BR.readLine();

        //add extra array element so we can later use it to store score
        inputValue = inputValue + " Score";

        String[] inputArr = inputValue.split(" ");

        int studentTotal = 0;

        for(int i = 3, i < inputArr.length; i++) {

            if(inputArr[i].equals["T"]) {

            studentTotal++; 
        }
    }

    //previous value of this is "Score" as we set earlier
    inputArr[13] = studentTotal;
    students.add(inputArr);
    }

    //Now do your printing here...
  }
}

5 Comments

Oh that make sense with the answers being the first input. I haven't learned ArrayLists yet but I'll check it out. Any idea why what I have is not splitting into separate elements by newlines?
Could you clarify what you mean?
If you mean string.split, have a look at this tutorial - examples.javacodegeeks.com/core-java/lang/string/…
I meant splitting into seperate indexes of the array by a new line. So the user enters in three students at once, each student in a new line and zzzz to end it. The array now has three elements, each element being all the students info. I thought the way I had it above did that but when I tried to print the first element to see if it was working all it printed was zzzz. Also, I think I understand your code except how does your array list differentiate each student entered, so that I can reference it when printing the output....does that make sense?
I also originally had an array of size 13 because there are 13 elements in each line for each student if that makes sense. I hope I'm not asking stupid questions lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.