2016-12-04 5 views
-1

충돌하고, 즉, catchNumberFormatException 그 프로그램 수익률을 작동하고 사용자에게 다시 메시지 표시하지 않는 경우.있는 Integer.parseInt()는 내가 <code>int</code>에 <code>String</code> 변환 <code>Integer.parseInt()</code>를 사용하려고 Java 프로그램

그러나 String 값을 입력하면 재발신 대신 프로그램이 정지됩니다.

keepLooping = true; 
    while(keepLooping) { 
     String unconvertedString = ""; 
     int convertedInt = 0; 
     try { 
     System.out.print("Enter a string to be parsed into an integer: "); 
     unconvertedString = userInput.next(); 
     convertedInt = Integer.parseInt(unconvertedString); 
     keepLooping = false; 
     } 
     catch (NumberFormatException e) { 
     userInput.next(); 
     } 
    } 
+0

그래서 int를 제공하면 올바르게 작동합니까? userInput은 정확히 무엇입니까? – Reisclef

+0

'next()'는 무엇이라고 생각하십니까? –

+0

'userInput'이란 무엇입니까? 'userInput.next()'의 기능은 무엇입니까? – YoungSpice

답변

0

당신은 캐치 절에 userInput.next()를 제거해야합니다

여기 내 코드입니다.

catch (NumberFormatException e) { 
    //userInput.next(); 
} 

예외가 발생하면 계속 반복하십시오.