英文:
Last line of textfile not storing correctly into 2D array
问题
我的扫描仪未能正确访问文本文件的最后一行,因此将文本文件的最后一行存储为全0的值存入我的二维数组中,而不是实际存在的值。我相信我已经提供了所有可以说明问题出在哪里的上下文信息,但如果需要更多信息,我可以更新这个问题,提前谢谢。
// 创建二维数组对象,从文件中存储值。
public DominoSort(String fileName) throws java.io.FileNotFoundException {
this.grid = new int[7][8]; // 二维数组,用于存储文本文件中的值
Scanner br = new Scanner(new File(fileName));
String line = br.nextLine();
int r = 0;
int c = 0;
while (br.hasNextLine) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
br.close();
}
// 打印网格的函数,打印出网格
public void printGrid() {
for (int r = 0; r < this.grid.length; r++) {
System.out.println();
for (int c = 0; c < this.grid[r].length; c++) {
System.out.print(this.grid[r][c] + " ");
}
}
System.out.println();
}
// 用于检查的驱动程序
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in); // 从用户输入中获取文本文件名
System.out.print("Enter the file name: ");
String fileName = input.next();
DominoSort dom = new DominoSort(fileName); // 此调用填充了二维数组对象
dom.printGrid(); // 打印二维数组以输出结果
}
用于测试/预期输出的文本文件:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
2 0 3 2 5 0 5 4 {注意这一行}
实际输出:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
0 0 0 0 0 0 0 0 {这一行不正确}
英文:
My scanner is not accessing last line of my text file correctly, thus is storing the last line of the text file as all 0's into my 2D array instead of what is actually there. I believe I have provided everything that would give context as to what is going wrong, but if more info is needed I can update this question, thanks in advance.
//Creates 2-d array object, stores values from file.
public DominoSort (String fileName) throws java.io.FileNotFoundException{
this.grid = new int [7][8]; //2-d array to hold values from text file
Scanner br = new Scanner(new File(fileName));
String line = br.nextLine();
int r = 0;
int c = 0;
while (br.hasNextLine) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
//this.setS = new ArrayList<>();
//this.pairMappings = new ArrayList<ArrayList<dominoLocation>>();
br.close();
}
//Print grid function, prints out the grid
public void printGrid() {
for(int r = 0; r < this.grid.length; r++) {
System.out.println("");
for(int c = 0; c < this.grid[r].length; c++) {
System.out.print(this.grid[r][c] + " ");
}
}
System.out.println("");
}
//Driver for checking
public static void main(String[] args) throws IOException {
// String line;
//System.out.println(new File(".").getAbsolutePath());
Scanner input = new Scanner(System.in); //get textfile name from user input
System.out.print("Enter the file name: ");
String fileName = input.next();
DominoSort dom = new DominoSort(fileName); //this call populates the 2-d array object
//dom.solvePuzzle(6);
dom.printGrid(); //prints 2d array for output
//dom.solvePuzzle(6);
}
text file used for testing / expected output:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
2 0 3 2 5 0 5 4 {Notice this line}
actual output:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
0 0 0 0 0 0 0 0 {this line is not right}
答案1
得分: 0
你的问题出在嵌套的 while/for 循环中。在所有行都被读取之前,它已经达到了结束条件。(在读取最后一行之前,nextLine() 方法没有更多的行可供读取)。你可以通过在文件末尾额外添加一行或两行来验证这一点,这将显示出最后几行。
有几种方法可以解决这个问题,其中一种方法是在 while 循环之后添加一个额外的 for 循环,用于单独计算最后一行:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
或者,作为另一种选择,在第一次运行时不增加 line 的值:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
if (r != 0)
line = br.nextLine();
r++;
c = 0;
}
英文:
Your problem lies within the nested while/for loop. It reaches the end condition before all the lines are read. (The nextLine() method doesn't have any more lines before you read the last line). You can see this by putting an extra 1 or 2 lines in your file at the very end, making it show the last lines.
There are a few ways to fix it, one of them is by just adding on an extra for loop after the while loop to compute the last line individually:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
or alternatively, don't increment the line on the first run:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
if (r != 0)
line = br.nextLine();
r++;
c = 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论