-1

我有一个文本文件- ExamFile3.txt:

A A A A A A B A A A B C A A A A C B D A
B D A A C A B A C D B C D A D C C B D A
B D A A C A B A C D B C D A D C C B D A

我想在我做的DriverExam课程中计算通过的人数和获得满分的人数。分数 -

String answers[] = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D",   "A", "D", "C", "C", "B", "D", "A"};//the exam solution

import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.*;
/*
 * http://www.java-examples.com/read-char-file-using-datainputstream
 */

public class DriverExam {
boolean passed; //passed if student passes exam
int allRightQuestions=0; //counts number of people having full marks
int lessThan5score=0; //counts number of people having < 5 scores
int lessThan10Score=0; //counts number of people having < 10 scores
int totalCorrect; //number of questions being answered correctly
int totalIncorrect; //number of questions being answered wrongly
int questionsMissed[][]; //array for storing the missed question numbers
int row = 0;
int column = 0;

char[] trueanswers = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
String answers[] = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};//the exam solution
String showanswers[] = {"B D A A C A B A C D B C D A D C C B D A"};
String[] realanswers = new String[20];

char[][] reply;
char[][] studentanswers = new char[30][20];//ignore
String[][] pupilanswers = new String[30][20];
String showresults[] = new String[30];
String results;

public DriverExam(Scanner inputFile) 
    {
    Scanner file = inputFile;


    if ( pupilanswers.length < 30)
    while (file.hasNext() && row < pupilanswers.length )
    {
        for ( int i = 0; i < pupilanswers.length; i++)
        {
        pupilanswers[row][column] = file.next();

        column++;   
        }

        row++;
        getallRight();      
    }
}

public void getallRight()
{
int startcolumn = 0;    
int beginrow = 0;
boolean found;
do
{
    if (pupilanswers[beginrow][startcolumn] == answers[startcolumn] )
    {
        passed = true;
        totalCorrect +=1;
        passNo();
        allRightQuestions +=1;
    }
    startcolumn++;  
    if ( startcolumn == 20)
    {
    startcolumn = 0;
    beginrow++;
            }
}
while(  row < pupilanswers.length && column < pupilanswers.length);
getallright();//counts number of people having full marks
}

public int getallright()
{
    return allRightQuestions;
}

public void checkLessThan5()
{

}

public int passNo()
{
    return totalCorrect;
}

public void sequentialSearch()
{
    int first = 0;
    int rows = 0;
    int subscript = -1;
    boolean found = false;

    while(!found && first < showresults.length)
    {
        if (answers[first] == showresults[rows])
        {
            found = true;
            subscript = rows;
            allRightQuestions +=1;
        }
        rows++;
    }

    while(!found && first < showresults.length)
    {
        if (showanswers[first] == pupilanswers[rows][column])
        {
            found = true;
            subscript = rows;
            allRightQuestions +=1;
        }
        rows++;
    }
    getallright();
}

}

您如何将上述文件中的这些字符串行处理成二维数组,我可以在其中将 String[] 答案与文件中的二维数组进行比较?

4

1 回答 1

0

从 Java 7 开始,您可以将文本文件加载到List. 据我所知,这是创建 String[][] 的最短方法

String[][] root;

List<String> lines = Files.readAllLines(Paths.get("ExamFile3.txt"), StandardCharsets.UTF_8);

root = new String[lines.size()][];

 lines.removeAll(Arrays.asList("", null)); // <- remove empty lines

for(int i =0; i<lines.size(); i++){
  root[i] = lines.get(i).split("[ ]+");
}

现在你已经填充root

希望对你有帮助

于 2013-10-05T09:12:26.430 回答