I have a problem over my application. I want the app to detect that if the user does not input any value for the string (in other words, just press enter after being asked to input something), the app then asks whether he/she wishes to quit the program or not.
Am I doing it right?
words = s.nextLine();
if (words.equals(null)) {
No, you’re not doing it right.
nextLine()will return an empty string if the user just hits return. As far as I can tell it will never returnnull. If the end of the input has been reached, it will throwNoSuchElementException(unlikeBufferedReader.readLine()which will return null in that case). So you want:or
or
… and use
hasNextLine()if you want to detect the end of input first.