Is it able for the code bellow to run correctly without creating a new Scanner object inside the loop?
Can it be done with just a definition of a Scanner object outside the loop(commented code)?
// Scanner sc = new Scanner(System.in);
while(true){
Scanner sc = new Scanner(System.in);
try{
int x = sc.nextInt();
System.out.printf("You gave %d\n", x);
}catch(Exception e){
System.out.println("Plz give a valid number!");
}finally{
sc.close();
}
}