NoobCoderChick
NoobCoderChick

Reputation: 627

String Arrays and BufferedReader

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);      
}

}

Upvotes: 2

Views: 524

Answers (2)

user3928919
user3928919

Reputation:

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.

Upvotes: 2

Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

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

Upvotes: 1

Related Questions