简体   繁体   中英

Java- How to compare 1D array with 2D array

I have trouble compare 1D array with 2D array. I already import the txt file to 1D array and 2D array. The 1D array contains 20 correct answer (True and False). The 2D array contains 2000 student answers (100 students * 20 answers per student).

I want to design a program that shows the score for each student, which I already tried to program. Can you help me to figure out which part I did wrong?

The second part of the program is to print out for each question, how many student got right for each question?

Thank you so much!!

import java.io.*;
import java.util.Arrays;

public class Scores
{
  public static void main(String[] args) {

  try {
    File AnswerFile = new File("/Users/shaovera/NetBeansProjects/Scores/QuestionAnswer.txt");
    FileReader AnswerReader = new FileReader(AnswerFile);
    BufferedReader answerreader = new BufferedReader(AnswerReader);

    String[] words = new String[20];
    for(int a = 0; a < words.length; a++) {
      words[a] = answerreader.readLine();
      System.out.println(words[a]);
    }
    answerreader.close();
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  try {
    File StudentFile = new File("/Users/shaovera/NetBeansProjects/Scores/StudentAnswer.txt");
    FileReader StudentReader = new FileReader(StudentFile);
    BufferedReader studentreader = new BufferedReader(StudentReader);

    String[][] table = new String[100][20];

    for (int i = 0; i < table.length; i++) {
      for(int j = 0; j < table[i].length; j++) {
        table[i][j] = studentreader.readLine();
        System.out.print(table[i][j] + " ");

      }
      System.out.println("");
    }
    studentreader.close();
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  int count=0;
  int student=0;    
  String[] words = new String[20];
  String[][] table = new String[100][20];
  for (int column = 0; column < words.length; column++) {
    for (int row = 0; row < 100; row++) {
      if (words[row] == table[row][column]) {
        count++; 
        student++;
        System.out.print("student#" + student + ":" + count);
      }
    } 
  }
}

I think this correct code (I wrote comment in line when you have problems)

public static void main(String[] args){
    String[]words= new String[20]; // before load from file
    String[][]table =new String[100][20]; // before load from file

try{
    File AnswerFile=new File("/Users/shaovera/NetBeansProjects/Scores/QuestionAnswer.txt");
    FileReader AnswerReader = new FileReader(AnswerFile);
    BufferedReader answerreader = new BufferedReader(AnswerReader);


    for(int a=0; a<words.length;a++){
        words[a]=answerreader.readLine();
        System.out.println(words[a]);
    }
    answerreader.close();
}
catch (Exception ex){
    ex.printStackTrace();
}

try{
    File StudentFile=new File("/Users/shaovera/NetBeansProjects/Scores/StudentAnswer.txt");
    FileReader StudentReader = new FileReader(StudentFile);
    BufferedReader studentreader = new BufferedReader(StudentReader);           

    for (int i = 0; i <table.length; i++) {
        for(int j=0;j < table[i].length; j++){
            table[i][j]= studentreader.readLine();
            System.out.print(table[i][j]+" ");

        }
        System.out.println("");
    }
    studentreader.close();
}

catch (Exception ex){
    ex.printStackTrace();
}

    for (int column = 0; column < words.length; column++) {
        int student = 0;
        for (int row = 0; row < table.length; row++) {                            
            if (Objects.equals(words[column],table[row][column])) {
                student++;
            }
        }            
        System.out.println("student#" + student + ":" + column);
    }
}

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