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;
}
}