For some reason my program is crashing when I am trying to get a user Input for a program I am writing. My class with my main in it looks like this.
package myQuiz;
import java.util.Scanner;
public class QuizApp {
public static void main(String[] args) {
String filename;
System.out.print("Enter a filename: ");
Scanner sc = new Scanner(System.in);
filename = sc.nextLine();
sc.close();
Quiz theQuiz = new Quiz(filename);
theQuiz.testingFunction();
}
}
Everything works up until I call the testingFunction and inside of it I ask for user input. This is what the testingFunction looks like and part of the Quiz class
package myQuiz;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
public class Quiz {
private ArrayList<Question> questions = new ArrayList<Question>();
private String fileName;
private boolean fileIsValid;
private int correct;
private int incorrect;
public Quiz(String theFile){
fileName = theFile;
fileIsValid = false;
correct = 0;
incorrect = 0;
loadQuestions(fileName);
}
public void testingFunction(){
System.out.println("Enter: ");
String input = "";
Scanner as = new Scanner(System.in);
input = as.nextLine();
as.close();
if(questions.get(0).checkAnswer(input) == true)
System.out.println("True");
else
System.out.println("False");
}
}
The error says this.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at myQuiz.Quiz.testingFunction(Quiz.java:41)
at myQuiz.QuizApp.main(QuizApp.java:15)
Line 41 is input = as.nectLine();
Line 15 is theQuiz.testingFunction();
Anybody know what's causing this to crash? Everything else works perfectly fine. Those are the only two spots I am getting user input so far.