gpt4 book ai didi

java - 尝试用Java逐行读取文件

转载 作者:行者123 更新时间:2023-11-30 06:58:19 26 4
gpt4 key购买 nike

大家好,我正在尝试创建一个问题类型的数组,但我认为在计算文本文件中的问题时遇到问题,文本文件有一行,然后是空行,然后是另一行,然后是空行,等等...我收到错误:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Question.countQuestions(Question.java:27) at Question.readAllQuestions(Question.java:44) at test.main(test.java:7)

文本文件示例:

enter link description here

这是我的代码:

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;
}

最佳答案

希望,这会有所帮助..!

为了避免线程“main”中出现异常java.util.NoSuchElementException:未找到行,请使用

while(text.equals(" "))
{
file.nextLine();
}

而不是

while(!text.equals(""))
{
file.nextLine();
}

并在 try..catch block 中编写代码,例如。

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;
}

关于java - 尝试用Java逐行读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com