2016-12-04 6 views
-1

はリターンというNumberFormatException私はintStringを変換するInteger.parseInt()を使用しようと、それが動作しない場合は、catchいますクラッシュし、ユーザーを求めるプロンプトは表示します。Integer.parseInt()は、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

あなたはcatch節でuserInput.next()を取り除く必要があります。

は、ここに私のコードスニペットです。

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

例外が発生した場合は、ループを継続してください。

関連する問題