A solution that never caused the error again for me was to use:
import java.util.Scanner;
String arbritraryString ="";
int arbritraryInteger =0;
boolean temporaryCondition =false;
Scanner keyboard =new Scanner(System.in);
do{
try{
arbritraryString =keyboard.nextLine();
arbritraryInteger =Integer.parseInt(arbritraryString);
/*
The rest of your code
*/
temporaryCondition =false;
}catch(NumberFormatException e){System.err.println("error"); temporaryCondition =true;}
}while(temporaryCondition);