如何在java中将文本文件中的字符串读入二维数组?

时间:2013-10-05 08:34:40

标签: java arrays string multidimensional-array 2d

我有一个文本文件 - 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();
}

}

如何从上面的文件处理这些Strings行到2D数组,在那里我可以将String []答案与文件中的2D数组进行比较?

1 个答案:

答案 0 :(得分: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

希望它能帮到你