从文本文件读取到java中的二维字符串数组

时间:2016-09-15 01:23:04

标签: java multidimensional-array

这类似于Reading 2-D array from a file。我正在尝试将文本文件读入java中的二维数组。不同之处在于我需要将其作为字符串数组而不是整数读入。 (是的,这是家庭作业。)我能够让代码用于整数但我的文本文件中可以包含“*”和“ - ”,这会导致.nextInt()抛出异常。我已经尝试使用.next()将它作为字符串拉出来,使用空格作为分隔符。然而,这也是从一开始就抛出异常。问题似乎在readFile函数中。如何将每个字符作为字符串拉入?这是我的3个功能:

    public static void main(String [] args) {

        Scanner s = new Scanner(System.in);
        String fileName = ""; //for javac
        //obtain file name for puzzle
        if(0 == args.length) {
            System.out.println("Welcome to the Sudoku Solver.");
            System.out.println("Please enter a file name.");
            fileName = s.nextLine();
        } else if(1 == args.length) {
            fileName = args[0];
        } else {
            System.out.println("You have entered invalid data.");
            System.exit(1);
        }

        //open puzzle file and read puzzle
        int m = 0; //for javac
        try {
            BufferedReader f = new BufferedReader(new FileReader(fileName));

            while(f.readLine() != null) {
                ++m;
            }
            System.out.println(m);
            String[][] theArray; 
            f.close();

            theArray = readFile(m, fileName);
            readPuzzle(theArray);

        } catch(Exception e) {
            System.out.println("An error has occurred...");
        }
    }

    public static void readPuzzle(String [][] thePuzzle) {

        for(int r = 0; r < thePuzzle.length; ++r) {
            for(int c = 0; c < thePuzzle[r].length; ++c) {
                System.out.printf("%7d", thePuzzle[r][c]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static String[][] readFile(int m, String fileName) {

        String[][] theArray = new String[m][m];
        try{
            Scanner g = new Scanner(new File(fileName));

            for(int r = 0; r <= theArray.length; ++r){
                for(int c = 0; c <= theArray[r].length; ++c){
                    if(g.hasNext()){
                        theArray[r][c] = g.next("\\s+");
                    }
                }
            }
        } catch(Exception e) {
            System.out.println("Error in readFile.");
        }

        return theArray;
    }

文本文件如下所示:

    5 3 * * 7 * * * *
    6 * * 1 9 5 * * *
    * 9 8 * * * * 6 *
    8 * * * 6 * * * 3
    4 * * 8 * 3 * * 1
    7 * * * 2 * * * 6
    * 6 * * * * * * *
    * * * 4 1 9 * * 5
    * * * * 8 * * 7 9

2 个答案:

答案 0 :(得分:0)

我认为不是使用Scanner.next(),而是想尝试组合BufferReader和String.split。

以下是示例代码:

BufferedReader f = new BufferedReader(new FileReader(fileName));
String[][] theArray = new String[9][9];  // << change array size as you wish
String line;
while((line = f.readLine()) != null) {
    theArray[m] = line.split(" ");   // Split text line by space
    ++m;
}

答案 1 :(得分:-1)

下面的代码与您期望的类似。请确保数据文件不包含任何不必要的空格。由于数组的大小目前是硬编码的,因此您必须记住保持大小(代码中的数组和文件中的数据)同步。

public static void main(String[] args) { try { Scanner input = new Scanner(new FileReader(new File("D:/Data File/Data.txt"))); int iRows = 9; int iCols = 9; String [][] strArr = new String[iRows][iCols]; for(int i = 0; i < iRows ; i++){ for(int j = 0; j < iCols ; j++){ strArr[i][j] = input.next(); } } input.close(); //Printing for(int i = 0; i < 9 ; i++){ for(int j = 0; j < 9 ; j++){ System.out.print(strArr[i][j] + " "); } System.out.println(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }