分析文件时出错--运行时错误 导入java.io.File; 导入java.util.Scanner; 导入java.io.FileNotFoundException; 导入java.lang.IllegalStateException; 导入java.util.NoSuchElementException; 导入java.util.List; 导入java.util.ArrayList; 导入java.util.StringTokenizer; 公共类数据库{ 字符串文件名; 扫描仪输入; 字符串数据[]; 列出记录; 公共数据库(字符串文件名){ fileName=“C:/Users/lucifer/Desktop/input.txt”; } 公共void openFile(){ 试一试{ 输入=新扫描仪(新文件(“C:/Users/lucifer/Desktop/input.txt”); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } 公共档案{ //将文件中的所有行(记录)读取到ArrayList中 记录=新的ArrayList(); 试一试{ while(input.hasNext()) records.add(input.nextLine()); 系统输出打印(记录); }捕获(例外e){ //TODO:处理异常 } } 公共字段(){ 字符串分隔符=“,\n”; //创建二维数组以保存数据(见Deitel,第313-315页) 文件中数组=#行的int rows=records.size();/#行 data=newstring[rows][];//为数组创建行 int行=0; for(字符串记录:记录){ StringTokenizer令牌=新的StringTokenizer(记录,分隔符); int cols=tokens.countTokens(); data[row]=新字符串[cols];//为当前行创建列 int col=0; while(tokens.hasMoreTokens()){ data[row][col]=tokens.nextToken().trim(); //系统输出打印(数据[行][列]+“”); col++; } } } 公共静态void main(字符串[]args){ 数据库文件1=新数据库(空); file1.openFile(); file1.readRecords(); file1.parseFields(); 打印矩阵(文件1.数据); } 静态void打印矩阵(字符串[][]网格){ 对于(int r=0;r
这里我使用scanner解析一个逗号分隔的文件。分析文件时出错--运行时错误 导入java.io.File; 导入java.util.Scanner; 导入java.io.FileNotFoundException; 导入java.lang.IllegalStateException; 导入java.util.NoSuchElementException; 导入java.util.List; 导入java.util.ArrayList; 导入java.util.StringTokenizer; 公共类数据库{ 字符串文件名; 扫描仪输入; 字符串数据[]; 列出记录; 公共数据库(字符串文件名){ fileName=“C:/Users/lucifer/Desktop/input.txt”; } 公共void openFile(){ 试一试{ 输入=新扫描仪(新文件(“C:/Users/lucifer/Desktop/input.txt”); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } 公共档案{ //将文件中的所有行(记录)读取到ArrayList中 记录=新的ArrayList(); 试一试{ while(input.hasNext()) records.add(input.nextLine()); 系统输出打印(记录); }捕获(例外e){ //TODO:处理异常 } } 公共字段(){ 字符串分隔符=“,\n”; //创建二维数组以保存数据(见Deitel,第313-315页) 文件中数组=#行的int rows=records.size();/#行 data=newstring[rows][];//为数组创建行 int行=0; for(字符串记录:记录){ StringTokenizer令牌=新的StringTokenizer(记录,分隔符); int cols=tokens.countTokens(); data[row]=新字符串[cols];//为当前行创建列 int col=0; while(tokens.hasMoreTokens()){ data[row][col]=tokens.nextToken().trim(); //系统输出打印(数据[行][列]+“”); col++; } } } 公共静态void main(字符串[]args){ 数据库文件1=新数据库(空); file1.openFile(); file1.readRecords(); file1.parseFields(); 打印矩阵(文件1.数据); } 静态void打印矩阵(字符串[][]网格){ 对于(int r=0;r,java,arrays,file,Java,Arrays,File,这里我使用scanner解析一个逗号分隔的文件。 为什么此代码在打印数据数组时引发异常??您没有更新解析字段中的行,请尝试 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.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();
}
}
}
在方法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++;
}
}
花了我一段时间,但我找到了:
在parseFields
中,启动int row=0;但不要在每个循环中都执行行+++
,以便覆盖同一行。当到达printMatrix
时,您尝试打印未初始化的行。然后你会得到一个例外
将解析字段更改为:
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
}
}
什么是例外?什么例外,具体在哪里?请用准确的错误/例外更新您的帖子
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++;
}
}