从文本文件到2Darray的读取表
我有一个java代码,在其中读取一个txt文件,然后迭代它,以便我可以在2D数组中填写。 阅读文件后,我能够打印出其内容,因此我确定该文件已被读取。而且我还确定,当发现一条线时,BufferedReader库的.hasnextline方法显示为True。 但是,当我在一段时间内使用它时,它的作用就好像没有发现的线一样,因此它没有迭代,hense,我不知道桌子上有多少行。==>
while (sc.hasNextLine()==true){ row++;}
此外,当我对行数进行核对编码时,我可以检查其他一切是否还好时,我没有发现一条错误。请帮助我。 我将链接下面的代码。
package com.company;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void
main(String args[]) throws Exception {
int row=0;
int column=0;
int count=0;
BufferedReader x = new BufferedReader(new FileReader("src\\Table.txt"));
Scanner sc = new Scanner(x);
System.out.println(sc.nextLine()+sc.hasNextLine()+"\n"+sc.nextLine()+sc.hasNext()+"\n"+sc.nextLine()+"\n"+sc.nextLine()+sc.hasNextLine());
while (sc.hasNextLine()==true){ row++;}
System.out.println(row);
for (int i=0; i<row; i++) {
String[] line = sc.nextLine().trim().split(",");
System.out.println(line);
for (String line1 : line) {
if (",".equals(line1)) {
count++;
}
count+=1;
if(count>column){
column=count;
}
}
}
String [][] myArray = new String[row][column];
for (int i=0; i<myArray.length; i++) {
String[] line = sc.nextLine().trim().split(",");
for (int j=0; j<line.length; j++) {
myArray[i][j]= line[j];
}
}
System.out.println(Arrays.deepToString(myArray));
}
}
我也得到了这个输出
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52205:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\acer pc\IdeaProjects\PDM\out\production\PDM" com.company.Main
CalculusII,Algebra,Networktrue
CalculusII,Algebra,Webtrue
CalculusIII,Prog2,Network
Algebra,Prog1,Webfalse
0
[]
Process finished with exit code 0
i have a java code where i read a txt file, then iterate it so that i can fill it in a 2d array.
after i read the file i was able to print out its contents so i was sure that the file was read. and i was also sure that the bufferedreader library's .hasNextLine method was showing true when a line was found.
but when i used it in a while loop, it just acted as if no lines where found, thus it didnt iterate, hense i couldn't know how many rows i had in the table.==>
while (sc.hasNextLine()==true){ row++;}
furthermore, when i hard-coded the number of rows so that i could check if everything else was ok, i got a line not found error. please help me out.
i will link the code below.
package com.company;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void
main(String args[]) throws Exception {
int row=0;
int column=0;
int count=0;
BufferedReader x = new BufferedReader(new FileReader("src\\Table.txt"));
Scanner sc = new Scanner(x);
System.out.println(sc.nextLine()+sc.hasNextLine()+"\n"+sc.nextLine()+sc.hasNext()+"\n"+sc.nextLine()+"\n"+sc.nextLine()+sc.hasNextLine());
while (sc.hasNextLine()==true){ row++;}
System.out.println(row);
for (int i=0; i<row; i++) {
String[] line = sc.nextLine().trim().split(",");
System.out.println(line);
for (String line1 : line) {
if (",".equals(line1)) {
count++;
}
count+=1;
if(count>column){
column=count;
}
}
}
String [][] myArray = new String[row][column];
for (int i=0; i<myArray.length; i++) {
String[] line = sc.nextLine().trim().split(",");
for (int j=0; j<line.length; j++) {
myArray[i][j]= line[j];
}
}
System.out.println(Arrays.deepToString(myArray));
}
}
i also get this output
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52205:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\acer pc\IdeaProjects\PDM\out\production\PDM" com.company.Main
CalculusII,Algebra,Networktrue
CalculusII,Algebra,Webtrue
CalculusIII,Prog2,Network
Algebra,Prog1,Webfalse
0
[]
Process finished with exit code 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行以下语句时,您已经读取了整个文件,因此没有剩下的行可读取。
这就是为什么您的关注迭代立即停止并且不显示预期输出的原因。也许,您期望的是扫描仪的内部光标能够在到达文件结束后重置本身。但是,这绝对不是
扫描仪
类(或其他任何)的行为,并且其任何方法都没有提供将扫描仪的光标重置或重新定位到文件的特定点的行为。您需要做的是关闭连接并重新建立它们,以重新启动阅读内容。例如,您可以在尝试块中包含每种文件消耗,以自动处理完成后的所有连接。
为了简化代码,在第一个循环中,您可以计算行数,并使用“最多列”检查行,而在第二个循环中,您可以重新阅读文件的内容。
When you perform the following statement you've already read your entire file, so there are no lines left to read.
This is why your following iterations stop immediately and don't show the expected output. Maybe, what you were expecting was for the Scanner's internal cursor to reset itself once the end of file was reached. However, this is definitely not the behavior of the
Scanner
class (or any other), and none of its methods offer to reset or reposition the Scanner's cursor to a specific point of the file.What you need to do is to close your connections and re-establish them in order to restart reading the content. For example, you could include every file consumption within a try block to automatically dispose every connection once you're done.
To simplify your code, in your first loop you could count the number of lines and check for the line with "the most columns", while in your second loop you could re-read the file's content.