Blank input from scanner - java

后端 未结 4 1988
梦毁少年i
梦毁少年i 2020-12-18 11:16

What I\'m trying to do is if the user clicks on the enter key the program should throw a BadUserInputException. My problem is whenever I press the enter key it just puts me

4条回答
  •  攒了一身酷
    2020-12-18 11:55

    The scanner looks for tokens between whitespaces and newlines - but there aren't any. I don't tend to use Scanner for reading from standard input - I use the old-fashioned BufferedReader method like this:

    BufferedReader buf = new BufferedReader (new InputStreamReader (System.in));
    

    and then I can say

    String line = buf.readLine ();
    if (line.equals ("")) blah();
    

    There may be an easier solution however.

提交回复
热议问题