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();
}
}
}
这里我使用scanner解析一个逗号分隔文件。 为什么这段代码在打印数据阵列时会抛出异常??
答案 0 :(得分:0)
您未在row
中更新parsefield
,请尝试
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++;
}
}
答案 1 :(得分: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
}
}
答案 2 :(得分: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++;
}
}