You close the second Scanner
which closes the underlying InputStream
, therefore the first Scanner
can no longer read from the same InputStream
and a NoSuchElementException
results.
The solution: For console apps, use a single Scanner
to read from System.in
.
Aside: As stated already, be aware that Scanner#nextInt
does not consume newline characters. Ensure that these are consumed before attempting to call nextLine
again by using Scanner#newLine()
.
See: Do not create multiple buffered wrappers on a single InputStream
Related Posts:
- What is a StackOverflowError?
- What is the proper way to handle a NumberFormatException when it is expected?
- NoSuchElementException with Java.Util.Scanner
- JavaFX – Exception in Application start method?
- Rock, Paper, Scissors Game Java
- JavaFX – Exception in Application start method? [duplicate]
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- EOFException – how to handle?
- Can I catch multiple Java exceptions in the same catch clause?
- Why am I getting InputMismatchException?
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- Scanner vs. BufferedReader
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- What does Scanner input = new Scanner(System.in) actually mean?
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- What’s the difference between next() and nextLine() methods from Scanner class?
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- java.util.NoSuchElementException: No line found
- How do I use a delimiter with Scanner.useDelimiter in Java?
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- How to create a custom exception type in Java?
- What does Scanner input = new Scanner(System.in) actually mean?
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- java.util.NoSuchElementException – Scanner reading user input
- Java can’t find file when running through Eclipse
- FXML Load exception
- StringIndexOutOfBoundsException String index out of range: 0
- Reading a .txt file using Scanner class in Java
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- Connection Java – MySQL : Public Key Retrieval is not allowed
- Why is a ConcurrentModificationException thrown and how to debug it
- Using a do-while loop to check a User’s input in Java
- Throwing multiple exceptions in Java
- Multiple delimiters in Scanner class of Java
- java.lang.ArrayIndexOutOfBoundsException: 0
- “Cannot be resolved to a type” when attempting to use Scanner
- Official reasons for “Software caused connection abort: socket write error”
- How can I read a text file in Android?
- Exception is never thrown in body of corresponding try statement
- Read next word in java
- What does hasNext() in a While do?
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- “Javac” doesn’t work correctly on Windows 10
- How to uninstall Eclipse?
- Error parsing data org.json.JSONException: Value
- Converting array to list in Java
- Java – What does “\n” mean?
- What does “|=” mean? (pipe equal operator)
- What does request.getParameter return?
- DB2 jdbc connection error Connection refused. ERRORCODE=-4499, SQLSTATE=08001
- Automated Python to Java translation
- How to fix array index out of bounds error?
- How do I analyze a .mdmp file?
- catDog string problem at Codingbat.com
- What is the difference between String.subString() and String.subSequence()
- Why Maven project is tied to J2SE-1.5 by default?
- Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
- implicit super constructor Person() is undefined. Must explicitly invoke another constructor?
- java.net.SocketTimeoutException: Read timed out under Tomcat
- How to resolve Error listenerStart when deploying web-app in Tomcat 5.5?
- package does not exist error!
- Required: Variable Found: Value
- What does the colon (:) operator do?
- Missing method body, or declare abstract in Java
- java code is showing error. ( ‘;’,expected) [closed]
- How can I edit a .jar file?
- java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
- JAVA_HOME should point to a JDK not a JRE
- This Activity already has an action bar supplied by the window decor?
- Convert hex string to int
- Orphaned Case Error in Java
- Java: how to initialize String[]?
- Regular Expressions on Punctuation
- How to make a countdown timer in Java
- (Java) Tic-Tac-Toe game using 2 dimensional Array
- Incompatible types List of List and ArrayList of ArrayList
- Dice Rolling java program
- Difference between string object and string literal
- Best way to create enum of strings?
- Why use getters and setters/accessors?
- How to use a Do-while loop that continuously prompts a user?
- Java equivalent of unsigned long long?
- Getting “unixtime” in Java
- How do I break out of nested loops in Java?
- When to use a Map instead of a List in Java?
- How do I replace a character in a string in Java?
- What’s so special about 0x7f?
- Append a single character to a string or char array in java?
- Preventing a Java class from being instantiated and inherited
- Collision Detection between two images in Java
- What does .pack() do?
- String is immutable. What exactly is the meaning?
- How do I autoindent in Netbeans?
- What is an attribute in Java?