尝试在Java中逐行读取文件
嗨,伙计们,我正在尝试创建一个问题类型数组,但我想我在计算文本文件中的问题时遇到了问题, 文本文件有一行,然后是空行,然后是另一行,然后是空行,所以。。。我得到一个错误: 线程“main”java.util.NoSuchElementException中的异常:无行 在java.util.Scanner.nextLine(未知源代码)中找到 问题.countQuestions(问题.java:27)见 test.main(test.java:7)中的Question.readAllQuestions(Question.java:44) 文本文件的一个示例: 这是我的密码:尝试在Java中逐行读取文件,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,嗨,伙计们,我正在尝试创建一个问题类型数组,但我想我在计算文本文件中的问题时遇到了问题, 文本文件有一行,然后是空行,然后是另一行,然后是空行,所以。。。我得到一个错误: 线程“main”java.util.NoSuchElementException中的异常:无行 在java.util.Scanner.nextLine(未知源代码)中找到 问题.countQuestions(问题.java:27)见 test.main(test.java:7)中的Question.readAllQuestio
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Question {
private String q; private String a;
private String b; private String c;
private String d; private String cA;
public Question(String q, String a, String b, String c, String d, String cA) {
this.q = q; this.a = a;
this.b = b; this.c = c;
this.d = d; this.cA = cA;
}
private static int countQuestions() throws FileNotFoundException{
int counter = 0;
Scanner file = new Scanner (new File("testBank.txt"));
while(file.hasNextLine()){
// check if line empty
String text = file.nextLine();
while(!text.equals("")){
file.nextLine();
}
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
counter++;
}
return counter;
}
public static Question[] readAllQuestions() throws FileNotFoundException{
int numberOfQuestions = countQuestions();
Question [] allQuestions = new Question[numberOfQuestions];
Scanner file = new Scanner (new File("testBank.txt"));
for (int i = 0 ; i < allQuestions.length ; i++){
String text = file.nextLine();
String q = "";
while(!text.equals("")){
q += file.nextLine();
}
String a=file.nextLine();
file.nextLine();
String b=file.nextLine();
file.nextLine();
String c=file.nextLine();
file.nextLine();
String d=file.nextLine();
file.nextLine();
String cA=file.nextLine();
file.nextLine();
Question question = new Question(q,a,b,c,d,cA);
allQuestions[i] = question;
}
return allQuestions;
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公开课问题{
私有字符串q;私有字符串a;
私有字符串b;私有字符串c;
私有字符串d;私有字符串cA;
公开问题(字符串q、字符串a、字符串b、字符串c、字符串d、字符串cA){
this.q=q;this.a=a;
这个.b=b;这个.c=c;
this.d=d;this.cA=cA;
}
私有静态int countQuestions()引发FileNotFoundException{
int计数器=0;
扫描仪文件=新扫描仪(新文件(“testBank.txt”);
while(file.hasNextLine()){
//检查行是否为空
String text=file.nextLine();
而(!text.equals(“”){
file.nextLine();
}
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
计数器++;
}
返回计数器;
}
公共静态问题[]readAllQuestions()引发FileNotFoundException{
int numberOfQuestions=countQuestions();
问题[]所有问题=新问题[numberOfQuestions];
扫描仪文件=新扫描仪(新文件(“testBank.txt”);
for(int i=0;i
希望,这会有帮助
为避免线程“main”java.util.NoSuchElementException中出现异常:未找到任何行,请使用
while(text.equals(" "))
{
file.nextLine();
}
而不是
while(!text.equals(""))
{
file.nextLine();
}
并在try..catch块中编写代码
private static int countQuestions() throws FileNotFoundException{
int counter = 0;
Scanner file = new Scanner (new File("testBank.txt"));
while(file.hasNextLine()){
try
{
// check if line empty
String text = file.nextLine();
while(text.equals(" ")){
file.nextLine();
}
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
counter++;
}
catch(NoSuchElementException e)
{
//Found End of File
}
}
return counter;
}
检查这个
public static Question[] readAllQuestions() throws FileNotFoundException
{
int numberOfQuestions = countQuestions();
Question [] allQuestions = new Question[numberOfQuestions];
Scanner file = new Scanner (new File("testBank.txt"));
try
{
for (int i = 0 ; i < allQuestions.length ; i++)
{
String text = file.nextLine();
String q = "";
while(text.equals(" ")){
file.nextLine();
}
q += text;
file.nextLine();
String a=file.nextLine();
file.nextLine();
String b=file.nextLine();
file.nextLine();
String c=file.nextLine();
file.nextLine();
String d=file.nextLine();
file.nextLine();
String cA=file.nextLine();
file.nextLine();
Question question = new Question(q,a,b,c,d,cA);
allQuestions[i] = question;
}
}
catch(NoSuchElementException e)
{
//Found End of File
}
return allQuestions;
}
publicstaticquestion[]readAllQuestions()抛出FileNotFoundException
{
int numberOfQuestions=countQuestions();
问题[]所有问题=新问题[numberOfQuestions];
扫描仪文件=新扫描仪(新文件(“testBank.txt”);
尝试
{
for(int i=0;i
在下面的代码中,您正在调用file.nextLine(),而不检查它是否有下一行。如果我没有错,您可能只想在循环中调用它一次
private static int countQuestions() throws FileNotFoundException{
int counter = 0;
Scanner file = new Scanner (new File("testBank.txt"));
while(file.hasNextLine()){
// check if line empty
String text = file.nextLine();
while(!text.equals("")){
file.nextLine(); // no check if new line exist
}
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
file.nextLine();
counter++;
}
请使用下面的代码
while(file.hasNextLine()){
// check if line empty
String text = file.nextLine();
while(!text.equals(" ")){
counter++;
}
}
在内部
循环中,当循环时,您没有更新文本,但您在循环条件中使用它,因此在循环过程中评估条件的结果不会改变,这将永远循环,甚至不会循环一次,这取决于进入循环之前文本
的值。thr中的相同问题例外ead“main”java.util.NoSuchElementException:Question.countQuestions(Question.java:32)at Question.test.main(test.java:7)处的java.util.Scanner.nextLine(未知源代码)处未找到任何行它起作用了,但我遇到了另一个问题,当我试图为第二个方法readAllQuestions()中的问题和答案分配变量时,当我运行代码Question[]Array=Question.readAllQuestions();Array.toString();(我已经创建了一个toString方法来返回问题和4个选项)时,它根本没有显示任何内容