简体   繁体   中英

reading two txt files in java that contain boolean values, and creating a 2d array

I am new to java, the code I am using was created with help of a tutor. I cannot seem to figure out why my output is not correct. For example, the number of answers answered correctly is not matching up to the txt files.

Design and implement an application that reads 2000 grades into a 2d array from responses.txt that represens results from a test of 20 true-false questions for 100 students. Each row of the array represents the answers of 1 of the 100 students. Each column represents the answers of each student to a specific question on test. Each cell of the array contains either Boolean value “true” or “false.” Read in values from another file key.txt to be stored in a 2nd 1d array of 20 Boolean values that represent correct answers to the 20 questions. Your program should compute and print out the # of correct answers for each student in chart form, the # of students who got each of the 20 questions correct in chart form, the avg quiz grade & the standard deviation of the grades.

responses.txt: (i am limited on characters in this post, so I am only posting 60 of the 2000 values):

true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true

key.txt :

true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true

my code :

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Grades {

   public static void main(String[] args) {
       // Student Data
       boolean[][] studentAnswers = new boolean[100][20];
       String responses = "C:\\Users\\kubert\\Documents\\NetBeansProjects\\TwoDArray\\src\\twodarray\\responses.txt";

       // Answers data
       boolean answers[] = new boolean[100];
       String answersFile = "C:\\Users\\kubert\\Documents\\NetBeansProjects\\TwoDArray\\src\\twodarray\\key.txt";
       int[] allCorrect = new int[100]; // stores indexes of students who scored all correct

       int[] score = new int[100]; // scores of students

       try {
           BufferedReader br = new BufferedReader(new FileReader(responses));
           int row = 0;
           String temp = "";
           while ((temp = br.readLine()) != null) 
           {
               if (row >= 100)
               {
                   break; // break loop if file contains more than 100 student entries

               }
               String[] tokens = temp.split(" "); // Assumming each line in txt
                                                   // file is one student and
                                                   // results are separated by
                                                   // space for each question.
               for (int i = 0; (i < tokens.length && i < 20); i++)
               {
                   studentAnswers[row][i] = Boolean.valueOf(tokens[i]);
               }
               row++;
           }
            } 
       catch (IOException e) 
       {
           System.out.println("ERROR : " + e);
       }

       // Reading answers from file
       try {
           BufferedReader br = new BufferedReader(new FileReader(answersFile));
           int index = 0;
           String temp = "";
           while ((temp = br.readLine()) != null) {
               answers[index] = Boolean.valueOf(temp); // Assuming each line in
                                                       // answers.txt file is
                                                       // answer for each
                                                       // question
               index++;
           }
       } catch (IOException e) {
           System.out.println("ERROR: " + e);
       }

       // Prints correct answers of each student
       for (int i = 0; i < 100; i++) {
           System.out.print("Student " + (i + 1) + " -> ");
           int noOfCorrect = 0;
           for (int j = 0; j < 20; j++) {
               if (studentAnswers[i][j] == answers[j]) {
                   System.out.print(j + "\t");
                   noOfCorrect++;
               } else {
                   System.out.print("-" + "\t");
               }
           }
           if (noOfCorrect == 20) {
               allCorrect[i] = i;
           }
           score[i] = noOfCorrect * 5;
           System.out.println("\nNo of correct answers : " + noOfCorrect);
           System.out.println("Grade Score : " + getGrade(score[i]));
       }

       // Average Grade Score and Standard Deviation
       HashMap<String, List<Integer>> map = new HashMap<>();
       map.put("A", new ArrayList<Integer>());
       map.put("B", new ArrayList<Integer>());
       map.put("C", new ArrayList<Integer>());
       map.put("D", new ArrayList<Integer>());
       map.put("F", new ArrayList<Integer>());

       for (int studentScore : score) {
           String grade = getGrade(studentScore);
           switch (grade) {
           case "A":
               map.get("A").add(studentScore);
               break;
           case "B":
               map.get("B").add(studentScore);
               break;
           case "C":
               map.get("C").add(studentScore);
               break;
           case "D":
               map.get("D").add(studentScore);
               break;
           case "F":
               map.get("F").add(studentScore);
               break;
           }
       }

       Set<String> keys = new TreeSet(map.keySet());
       for (String key : keys) {
           System.out.println("Standard deviation " + key + " : " + printSD(map.get(key)));
       }
   }

   /*
   * To calculate the standard deviation of those numbers: 1. Work out the
   * Mean (the simple average of the numbers) 2. Then for each number:
   * subtract the Mean and square the result. 3. Then work out the mean of
   * those squared differences. 4. Take the square root of that and we are
   * done!
   */
   public static double printSD(List<Integer> list) {
       double sum = 0;

       if (list.size() == 0)
           return 0.0;
       for (int val : list)
           sum += val;
       double mean = sum / list.size();

       for (int i = 0; i < list.size(); i++) {
           sum += Math.pow((list.get(i) - mean), 2);
       }
       if (sum == 0)
           return 0.0;
       return Math.sqrt(sum / list.size());
   }

   public static String getGrade(int score) {
       String grade = "";
       if (score >= 90)
           grade = "A";
       else if (score >= 80)
           grade = "B";
       else if (score >= 70)
           grade = "C";
       else if (score >= 60)
           grade = "D";
       else
           grade = "F";

       return grade;
   }

}

the output I am currently getting:

run:
Student 1 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 2 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 3 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 4 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 5 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 6 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 7 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 8 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 9 -> 0  -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 10 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 11 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 12 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 13 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 14 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 15 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 16 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 17 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 18 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 19 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 20 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 21 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 22 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 23 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 24 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 25 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 26 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 27 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 28 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 29 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 30 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 31 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 32 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 33 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 34 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 35 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 36 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 37 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 38 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 39 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 40 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 41 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 42 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 43 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 44 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 45 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 46 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 47 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 48 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 49 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 50 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 51 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 52 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 53 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 54 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 55 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 56 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 57 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 58 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 59 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 60 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 61 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 62 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 63 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 64 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 65 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 66 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 67 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 68 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 69 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 70 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 71 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 72 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 73 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 74 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 75 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 76 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 77 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 78 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 79 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 80 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 81 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 82 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 83 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 84 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 85 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 86 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 87 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 88 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 89 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 90 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 91 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 92 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 93 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 94 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 95 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 96 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 97 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 98 -> 0 -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Student 99 -> - -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 5
Grade Score : F
Student 100 -> 0    -   -   -   -   -   -   -   -   -   10  -   12  -   14  -   16  -   18  -   
No of correct answers : 6
Grade Score : F
Standard deviation A : 0.0
Standard deviation B : 0.0
Standard deviation C : 0.0
Standard deviation D : 0.0
Standard deviation F : 5.782516753110189
BUILD SUCCESSFUL (total time: 1 second)

Looking at the Responses.txt file it appears that there are 2000 lines? Or are there 100 lines with 20 separated values (this is how you have your implementation)?

if (row >= 100)
{
   break; // break loop if file contains more than 100 student entries
}
String[] tokens = temp.split(" "); // Assumming each line in txt
                                   // file is one student and
                                   // results are separated by
                                   // space for each question.

Your file has 1 input per line for 2000 lines. The way your logic is now, is setup if there were 100 lines with 20 inputs (separated by space).

Also, change your 2d array to type of 'Object'.

This is pseudo-code:

       int row = 0;
       String temp = "";
       int currStudent = 0;
       //will terminate when file is complete
       while ((temp = br.readLine()) != null) 
       {
           if (row < 20)
           {
               //add answer to student
               //add 1 to row
           } else {
               //add 1 to student
               //add answer to student
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM