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?