2016-11-29 9 views
-1

データ型変換エラー

import java.util.Scanner; 

public class Solution { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in) ; 
     int number = input.nextInt() ; 

     String word = input.nextLine(); 

     String word1 = Integer.toString(number) ; 
     int number1 = Integer.parseInt(word) ; 

     if (number1 == Integer.parseInt(word)) 
      { 
      number1 = number + 10 ; 
      System.out.println("Congratualtion its a number"+" "+ number1) ; 
     } 

      else if (word1 == Integer.toString(number)) 
       { word1 = 10 + word ; 
      System.out.println("Congratualtion its a word"+" "+ word1); 
    } 
     else 
      System.out.println("Something is wrong !!! ") ; 

} 
} 

予期しないエラーを取得すると、エラーメッセージ:これは問題の行である

Error : Exception in thread "main" java.lang.NumberFormatException: For input string: " " 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:481) 
    at java.lang.Integer.parseInt(Integer.java:527) 
    at Solution.main(Solution.java:13) 
+2

** NumberFormatException:入力文字列の場合: "** **これに気付きましたか?データを正しく入力してください。 – developer

+0

予期しない理由は何ですか?別のエラーが予想されましたか? – shmosel

答えて

0

int number1 = Integer.parseInt(word) ;

wordは、数値以外の文字列です。コードはそれをできない整数に変換しようとしています。あなたはinputから得たそれぞれのものを印刷して、自分が持っていると思っていることを確かめたいかもしれません。

幸運。

+0

なぜ賛成投票ですか? –

関連する問題