Ammar
Ammar

Reputation: 31

Trying to reading a file line by line in Java

Hi guys i'm trying to create an array of type Question but i think i have a problem in counting the questions in the text file, the text file has a line then empty line then another line then an empty line and so... And i get an error:

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)

an example of the text file :

enter link description here

here's my code:

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

Upvotes: 1

Views: 92

Answers (2)

Pradnyarani
Pradnyarani

Reputation: 302

Hope, This will help..!

To avoid Exception in thread "main" java.util.NoSuchElementException: No line found use

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

instead of

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

and write your code in try..catch block eg.

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

Check this one

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

Upvotes: 1

Priya Singh
Priya Singh

Reputation: 66

In the below code you are calling file.nextLine() without checking if it has next line. Also if I am not wrong you might want to call it once only inside the loop.

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

Use below code instead

while(file.hasNextLine()){
    // check if line empty
    String text = file.nextLine();
    while(!text.equals(" ")){
         counter++;
    }       
}

Upvotes: 0

Related Questions