Issue with java Scanner not taking nextLine on new instance

package sandbox2; import java.util.Scanner; public class Sandbox2 { public static void main(String[] args) { for (int i = 0; i < 5; i++) { String s = askForProperty("Enter value for " + i + ": "); System.out.println(i + " is: " + s); } } private static String askForProperty(String message) { Scanner keyboard = new Scanner(System.in); System.out.print(message); String s = keyboard.nextLine(); keyboard.close(); return s; } }

When i run the above code, it returns the first response PERFECTLY. When it tries to ask for the second response, it returns:

java.util.NoSuchElementException: No line found

Why would it return this error? Each time the method askForProperty is called, the Scanner is a completely new instance! Does it have something to do with System.in as an input stream?

Answer1:

Define your scanner as a class variable and then close it only after you are done with all iterations. In your current setup, when you call keyboard.close you are also closing System.in which makes it unusable later on.

package sandbox2; import java.util.Scanner; public class Sandbox2 { static Scanner keyboard = new Scanner(System.in); // One instance, available to all methods public static void main(String[] args) { for (int i = 0; i < 5; i++) { String s = askForProperty("Enter value for " + i + ": "); System.out.println(i + " is: " + s); } keyboard.close(); //Only close after everything is done. } private static String askForProperty(String message) { System.out.print(message); String s = keyboard.nextLine(); return s; } }

Answer2:

Closing a Scanner causes the underlying InputStream to be closed also. As there is only one System.in, any newly created Scanner objects will not be able to read from the same stream:

keyboard.close();

Close the Scanner in the last.

Answer3:

So,

The chief problem in your code is that you create and close Scanner immediately in every single iteration. That simply does not work. Imagine Scanner as a large connection to your IO that requires quite some assemblage. If you open / close it every single time - You might just find a case where the next command is fired before the connection is opened once again. It's very similar to what you might find in a DB connection as well. The way to prevent it is to have Scanner open BEFORE you start iterating, finish the loop and then close it.

Hence, remove the close() statement from the askForProperty() function and move it to your main. Pass the Scanner keyboard object to the function. Once all the iterations are over - Then close it.

import java.util.Scanner; public class Sandbox2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // Initialize the Scanner for (int i = 0; i < 5; i++) { String s = askForProperty("Enter value for " + i + ": ", keyboard); // Pass the Scanner System.out.println(i + " is: " + s); } keyboard.close(); // Close the Scanner now that the work is done. } private static String askForProperty(String message, Scanner keyboard) { System.out.println(message); String s = keyboard.nextLine(); return s; } }

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Issue with java Scanner not taking nextLine on new instance