简体   繁体   English

在Java中读取两个包含布尔值的txt文件,并创建一个2d数组

[英]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. 我是java的新手,我正在使用的代码是在辅导员的帮助下创建的。 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. 例如,正确回答的答案数量与txt文件不匹配。

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. 设计并实现一个应用程序,该应用程序从response.txt中将2000年的成绩读取到2d数组中,该数组表示100名学生对20个真假问题的测试结果。 Each row of the array represents the answers of 1 of the 100 students. 数组的每一行代表100个学生中的1个答案。 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. 数组的每个单元格都包含布尔值“ true”或“ false”。从另一个文件key.txt中读取值,以将其存储在第二个1d数组中,该数组包含20个布尔值,表示对20个问题的正确答案。 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. 您的程序应该以图表的形式计算并打印出每个学生的正确答案的数量,以图表形式对20个问题中的每一个都正确的学生的数量,平均测验成绩和成绩的标准偏差。

responses.txt: (i am limited on characters in this post, so I am only posting 60 of the 2000 values): response.txt :(我在这篇文章中的字符受限制,所以我只张贴了2000个值中的60个):

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 : 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? 查看Responses.txt文件,似乎有2000行? Or are there 100 lines with 20 separated values (this is how you have your implementation)? 还是有100行包含20个单独的值(这就是实现方式)?

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. 您的文件每行有1个输入(共2000行)。 The way your logic is now, is setup if there were 100 lines with 20 inputs (separated by space). 如果有100条线和20条输入(以空格分隔),则设置您现在的逻辑。

Also, change your 2d array to type of 'Object'. 另外,将2d数组更改为“对象”的类型。

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
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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