1
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class database {
    String fileName;
    Scanner input;
    String data[][];
    List<String> records;

    public database(String fileName) {
        fileName = "C:/Users/lucifer/Desktop/input.txt";
    }

    public void openFile() {
        try {
            input = new Scanner(new File("C:/Users/lucifer/Desktop/input.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void readRecords() {
        // Read all lines (records) from the file into an ArrayList
        records = new ArrayList<String>();
        try {
            while (input.hasNext())
                records.add(input.nextLine());
            System.out.println(records);

        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void parseFields() {
        String delimiter = ",\n";

        // Create two-dimensional array to hold data (see Deitel, p 313-315)
        int rows = records.size(); // #rows for array = #lines in file
        data = new String[rows][]; // create the rows for the array
        int row = 0;

        for (String record : records) {
            StringTokenizer tokens = new StringTokenizer(record, delimiter);
            int cols = tokens.countTokens();
            data[row] = new String[cols]; // create columns for current row
            int col = 0;
            while (tokens.hasMoreTokens()) {
                data[row][col] = tokens.nextToken().trim();
                // System.out.print(data[row][col] + " ");
                col++;
            }



        }

    }

    public static void main(String[] args) {

        database file1 = new database(null);
        file1.openFile();
        file1.readRecords();

        file1.parseFields();
            printMatrix(file1.data); 

    }

    static void printMatrix(String[][] grid) {
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[r].length; c++)
                System.out.print(grid[r][c] + " ");
            System.out.println();
        }
    }

}

Here i am parsing a comma separated file using scanner . why this code is throwing exception on printing data array??

4

3 回答 3

0

您没有更新rowparsefield请尝试

public void parseFields() {
        String delimiter = ",\n";

        // Create two-dimensional array to hold data (see Deitel, p 313-315)
        int rows = records.size(); // #rows for array = #lines in file
        data = new String[rows][]; // create the rows for the array
        int row = 0;

        for (String record : records) {
            StringTokenizer tokens = new StringTokenizer(record, delimiter);
            int cols = tokens.countTokens();
            data[row] = new String[cols]; // create columns for current row
            int col = 0;
            while (tokens.hasMoreTokens()) {
                data[row][col] = tokens.nextToken().trim();
                // System.out.print(data[row][col] + " ");
                col++;
            }
            row++;
        }

    }
于 2013-08-20T09:01:49.057 回答
0

在方法 parseFields() 中,您需要添加 row++;

public void parseFields() {
    String delimiter = ",\n";

    // Create two-dimensional array to hold data (see Deitel, p 313-315)
    int rows = records.size(); // #rows for array = #lines in file
    data = new String[rows][]; // create the rows for the array
    int row = 0;

    for (String record : records) {
        StringTokenizer tokens = new StringTokenizer(record, delimiter);
        int cols = tokens.countTokens();
        data[row] = new String[cols]; // create columns for current row
        int col = 0;
        while (tokens.hasMoreTokens()) {
            data[row][col] = tokens.nextToken().trim();
            // System.out.print(data[row][col] + " ");
            col++;
        }

     row++; //////////////Here

    }

}
于 2013-08-20T09:02:01.840 回答
0

花了我一段时间,但我找到了:

parseFields你启动 int row = 0; 但不要row++在每个循环中都这样做,以便覆盖同一行。当您到达时,printMatrix您尝试打印未初始化的行。然后你得到例外。

更改parseFields为:

public void parseFields() {
    String delimiter = ",\n";

    // Create two-dimensional array to hold data (see Deitel, p 313-315)
    int rows = records.size(); // #rows for array = #lines in file
    data = new String[rows][]; // create the rows for the array
    int row = 0;

    for (String record : records) {
        StringTokenizer tokens = new StringTokenizer(record, delimiter);
        int cols = tokens.countTokens();
        data[row] = new String[cols]; // create columns for current row
        int col = 0;
        while (tokens.hasMoreTokens()) {
            data[row][col] = tokens.nextToken().trim();
            // System.out.print(data[row][col] + " ");
            col++;
        }

        row++;
    }
}
于 2013-08-20T09:02:02.860 回答