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

标签 java arrays java.util.scanner

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

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/

相关文章:

java - 如何让 Maven 3.0.4 使用 Java7?

java - 计算程序运行时间?

java - 二维数组,输出不正确

python - 如何获得 numpy 掩码数组中的掩码行数?

java - Scanner#nextFloat 的奇怪行为

java - 将txt文件的不同行读取到不同的ArrayList中

java - 未找到 Ant 目标 1.6 包

java - 所有不带参数的方法都可以访问变量吗?

java - Java中的深拷贝数组

Java.util.scanner 似乎只读取某些行