사용자가 입력을 받아들이는 프로그램을 만들고 있습니다. 사용자가 잘못된 명령을 입력하면 입력 옵션을 다시 실행하고 싶습니다. 여기서는 정수 입력 만 선호하지만 사용자가 텍스트를 입력하면 예외를 catch하고 메서드를 다시 실행하려고합니다.java에서 예외 발생 후 메소드를 재 시도하는 방법은 무엇입니까?
나는 이것을 시도했지만 무한 루프로 변하게된다. 이것을 달성하는 가장 좋은 방법은 무엇일까? 누군가가 나를 돕는다면 좋을 것입니다. 당신이 정수가 아닌 다른 뭔가를 입력하면
boolean retry = true;
while(retry){
try{
//considering response from the user
System.out.print("\nEnter Your Command : ");
f_continue = processresponse(scanner.nextInt());
retry = false;
}catch(InputMismatchException e){
System.err.println("\nPlease Input a Valid Command from 1-5 or 0 for Exit");
}
}
스캐너 입력 처리에 대한 자세한 내용은 http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – azurefrog