0

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.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
Programmer
  • 105
  • 2
  • 11
  • [Java Doc](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()) and it says *NoSuchElementException - if no line was found* – Naman Mar 08 '17 at 07:21
  • You're closing the scanner. That closes System.in. Then you try to read from System.in again. But it's closed. Don't close scanners reading from System.in. – JB Nizet Mar 08 '17 at 07:22
  • So how should I get user input? – Programmer Mar 08 '17 at 07:23
  • You should also call `sc.hasNextLine()` before `sc.nextLine()`. – Jeremy Mar 08 '17 at 07:23
  • If I don't close it, eclipse flags that as a warning. And the ``sc.hasNextLine()`` didn't help, still crashes – Programmer Mar 08 '17 at 07:26
  • @Programmer, then close it when your program ends, rather than in the middle. – Jeremy Mar 08 '17 at 07:28
  • You can only close the Scanner once because it will close `System.in` with it. This stream can't be open again later, it is generated by the JVM (basicly). So only close the Scanner at the end of the process, so a instance variable Scanner would be bettter – AxelH Mar 08 '17 at 07:36

0 Answers0