Rachel
Rachel

Reputation: 73

Inability to read data from a .txt file into an ArrayList because of a NoSuchElement exception

I have to create a program that quizzes the user on 10 multiple choice questions that must be read into the main class from a .txt file.

To do this so far, I've assigned the data in the .txt file to 10 Question objects with a while loop (under the hasNextLine condition). The Questions are then supposed to be stored in an ArrayList.

However, I am getting an exception that says "java.util.NoSuchElementException: No line found" at the point where the Scanner allegedly skips from the blank line to the next question. (fileInput.nextLine)

To clarify, the format of the text file is:

I know that NoSuchElement exceptions happen when there is nothing left for the Scanner to read, but there is still data in the file that the Scanner should be reading even though it skips over the blank line.

I tried to move the for-loop (the one that puts the quizQuestion objects into the ArrayList) outside of the while loop, but then I get the error that the "quizQuestion" symbol can't be found. I also tried to make the while loop have the condition hasNextLine && index < questionList.size, which was effectively combining the two processes, but that caused it to skip to the very bottom of my program (the part that displays a goodbye message...not pictured) and not display any of the questions either. I have checked my .txt file and it's definitely formatted correctly so I don't know what the issue is at this point.

Here's my code:

// Create a new PrintWriter object and Scanner object that will read data from the file. 
        File quizFile = new File("fileWithTheQuestions.txt");
        Scanner fileInput = new Scanner(quizFile);

        // Create an ArrayList which will store the Question objects    
        ArrayList<myQuestion> questionList = new ArrayList<myQuestion>();

        /* Assign the data about the questions in the .txt file to 10 Question objects in the correct order.
        Also, read the Questions into the ArrayList. */
            while(fileInput.hasNextLine())
            {
                // Assign the values in the .txt file to variables.
                String question = fileInput.nextLine();
                String choiceA = fileInput.nextLine();
                String choiceB = fileInput.nextLine();
                String choiceC = fileInput.nextLine();
                String choiceD = fileInput.nextLine();
                String correctAns = fileInput.nextLine();

                String incorrectAns1 = fileInput.nextLine();
                String incorrectAns2 = fileInput.nextLine();
                String incorrectAns3 = fileInput.nextLine();

                fileInput.nextLine(); //skips over blank space in the file. I think this is where the issue is!!!!!

                // Use the variables as parameters for the Question objects' constructor. 
                myQuestion quizQuestion = new myQuestion(question, choiceA, choiceB, choiceC, choiceD, correctAns, incorrectAns1, incorrectAns2, incorrectAns3);

                // Read the Questions into the ArrayList.
                for (index = 0; index < questionList.size(); index++)
                {
                    questionList.add(quizQuestion);
                } 
            }

        // Close the Scanner after all data has been read into the main class.
        fileInput.close();

Ideally, the questions will print in a randomized order (using Collections.shuffle) which I have gotten to work successfully in a separate program when I was not using File I/O to import the data about the questions.

I'm pretty sure the problem is with the File I/O, but could it also be with the creation of the Question objects or the ArrayList?

Thanks anyone who can help with this.

EDIT: Here is the link to the .txt file.

Upvotes: 0

Views: 62

Answers (2)

Hero Wanders
Hero Wanders

Reputation: 3292

The error occurs when reaching the last line of your file. Your input file should either contain a trailing empty line or you should test for the existence of the blank line:

if (fileInput.hasNextLine()) {
    fileInput.nextLine();
}

Upvotes: 0

VSB
VSB

Reputation: 327

Class for the Question object

public class myQuestion {

String question;
String choiceA; 
String choiceB;
String choiceC;
String choiceD;
String correctAns;
String incorrectAns1;
String incorrectAns2;
String incorrectAns3;

public myQuestion(String question, String choiceA, String choiceB, String choiceC, String choiceD, String correctAns, String incorrectAns1, String incorrectAns2, String incorrectAns3) {
    this.question = question;
    this.choiceA = question; 
    this.choiceB  = choiceB;
    this.choiceC = choiceC;
    this.choiceD = choiceD;
    this.correctAns = correctAns;
    this.incorrectAns1 = incorrectAns1;
    this.incorrectAns2 = incorrectAns2;
    this.incorrectAns3 =incorrectAns3;
}

@Override
public String toString() {
    return this.question + 
    this.choiceA +
    this.choiceB +
    this.choiceC +
    this.choiceD +
    this.correctAns +
    this.incorrectAns1 +
    this.incorrectAns2 +
    this.incorrectAns3 ;
}

}

Class for scanning the text data

public class FileScanning {

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub

    File quizFile = new File("resources/fileWithTheQuestions.txt");
    Scanner fileInput = new Scanner(quizFile);
    fileInput.useDelimiter("\\R+");

    // Create an ArrayList which will store the Question objects    
    ArrayList<myQuestion> questionList = new ArrayList<myQuestion>();

    /* Assign the data about the questions in the .txt file to 10 Question objects in the correct order.
    Also, read the Questions into the ArrayList. */
    while(fileInput.hasNext())
    {

        String question = fileInput.next();
        String choiceA = fileInput.next();
        String choiceB = fileInput.next();
        String choiceC = fileInput.next();
        String choiceD = fileInput.next();
        String correctAns = fileInput.next();

        String incorrectAns1 = fileInput.next();
        String incorrectAns2 = fileInput.next();
        String incorrectAns3 = fileInput.next();

        // Use the variables as parameters for the Question objects' constructor. 
        myQuestion quizQuestion = new myQuestion(question, choiceA, choiceB, choiceC, choiceD, correctAns, incorrectAns1, incorrectAns2, incorrectAns3);

        questionList.add(quizQuestion);

    }

    // Close the Scanner after all data has been read into the main class.
    fileInput.close();

    //printing the content scanned above
    for(myQuestion quizQuestion : questionList) {
        System.out.println(quizQuestion.toString());
    }


}

}

I have used your sample input only.

Upvotes: 1

Related Questions