2014-04-16 4 views
-1

간단한 채팅 응용 프로그램을 구축하고 있는데 JOptionPane을 통해 서버 주소와 포트를 설정하려고합니다. 사용자가 포트 번호를 설정할 때 문자를 입력하지 않았는지 확인하고,JOptionPane InputMismatchException

int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!"); 

나는 try/catch 블록으로 처리하려고했으나 아무런 제안이 없었습니까?

+1

"try/catch 블록으로 처리하려했지만 작동하지 않았습니다"라는 의미는 무엇입니까? – BackSlash

+0

'try {code} catch (inputmismatchexception) {}'@BackSlash – xhulio

+0

그리고 작동하지 않는 것은 무엇입니까? – BackSlash

답변

1

NumberFormatException이 분명히 대답이기 때문에이 질문에 답이 표시되지 않습니다.

try{ 
    int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!"); 
}catch(NumberFormatException e){ 
    //handle error 
} 
+0

올바른 결과가 나올 때까지 JOptionPane을 표시하는 방법을 알고 계십니까? , 오류 메시지가 나타나면 입력을 다시 입력하십시오. – xhulio

+1

저는 이것을 위해 DocumentFilter를 사용했습니다. 그것은 아주 잘 작동합니다. 다음은 예제 링크입니다. http : //stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers –