-1
public static char[] puzzleInput() {
    printEnterPuzzleMessage();
    Scanner puzzleS = new Scanner(System.in);
    if(puzzleS.hasNext()) {
        char[] puzzle = puzzleS.next().toCharArray();
        while(!isLegalPuzzleStructure(puzzle)) {
            printIllegalPuzzleMessage();
            puzzleInput();
        }
        return puzzle;  
    }
    puzzleS.close();
    return null;
}

public static void main(String[] args) throws Exception{ //Q - 8

    Scanner fileName = new Scanner(System.in);
    if(!fileName.hasNext()) {
        System.out.println("No argument has been received");
        System.exit(0);
    }
    String filePath = fileName.nextLine();
    fileName.close();
    Scanner vocabulary = new Scanner(new File(filePath));
    String[] vocabularyArr = scanVocabulary(vocabulary);
    vocabulary.close();
    printReadVocabulary(filePath, vocabularyArr.length);
    printSettingsMessage();
    printEnterPuzzleMessage();  
    char[] puzzle = puzzleInput();

Hi, a beginner in Java is here. In the function puzzleInput, I open a Scanner to get an input from the user. For some reason, the program won't give me a chance to put in input, and therefor the argument (puzzle) gets a null as default, and later when puzzle is needed not as a null - throws a NullPointerException. There are many other functions in the code, but most of them are just a print commands, and the ones who are not were being checked by me, and are OK. The problem is just the scanner won't give me a chance to put in an input. Some points I'd like to clarify further: 1. The first Scanner (fileName) is not being skipped by the program, and I'm able to give it an argument. 2. I made sure I closed all the other scanners i've opened before. Can someone explain me what I'm doing wrong?

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Have you tried simplifying to: `Scanner puzzleS = new Scanner(System.in);` `String test = puzzleS.next();` If this works, then you can pinpoint the issue faster. – Evan Weissburg Nov 18 '17 at 20:30
  • Do you mean in puzzleInput? this is what I wrote... – Noam Rosenberg Nov 18 '17 at 20:32
  • `close()` method along with closing Scanner is also closing resource from which scanner was reading its data, in your case `System.in`. You can't read from closed resource any longer, even if you create another Scanner on it. So don't invoke `close()` until you are sure your application doesn't need to read from System.in. Also don't create two Scanners for same resource. Create one and pass it to other methods if they need it. – Pshemo Nov 18 '17 at 20:36
  • Maybe OT but you have a recursion in your `puzzleInput()` and do not use its return value. – sinclair Nov 18 '17 at 20:38
  • Be sure to accept an answer if it was helpful. – Gray Dec 09 '17 at 20:55

2 Answers2

1

program won't give me a chance to put in input

Your problem is that you are closing your Scanner in main:

Scanner fileName = new Scanner(System.in);
...
fileName.close();

This in turn closes the System.in input-stream which then cannot be reused in your puzzleInput() method because it is already closed. The right thing to do here is to pass in the Scanner variable into your puzzleInput() method and continue to reuse it there and not try to open up a new Scanner.

public static char[] puzzleInput(Scanner scanner) {
    printEnterPuzzleMessage();
    if(scanner.hasNext()) {
    ...
    // don't close it here
    return null;
}
...
Scanner scanner = new Scanner(System.in);
...
puzzleInput(scanner);

Couple of other comments:

  • Calling a Scanner fileName is not a good pattern. Choosing good names for your variables will help make the code self-documenting. scanner would be a better name of course.
  • When dealing with any input/output, it is a good practice to wrap any opening method in a try/finally block so it gets close properly. See also the try-with-resources functionality added in Java 7.
Gray
  • 115,027
  • 24
  • 293
  • 354
0

If you want a chance to do something with the input with a prompt, why not assign it to a String variable? This allows you to manipulate the input however you want later on too.

String input = scannerName.nextLine();
Hycyber
  • 13
  • 3