2016-08-13 154 views
-8
import java.util.*; 

class rev_sum { 

    public static void main(String args[]) { 
     int a, i, n, m = 0, t = 0; 
     char c; 
     String w, p = " "; 
     Integer intCost = 0; 
     try { 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Enter the number"); 
      n = sc.nextInt(); 
      w = Integer.toString(n); 
      a = w.length(); 
      for (i = 0; i < a; i++) { 
       c = w.charAt(i); 
       p = c + p; 
      } 
      intCost = Integer.parseInt(p); 
      m = n + intCost; 
      System.out.println("The sum of the original no and reversed number =" + m); 
     } catch (NumberFormatException ex) { 
     } 
    } 
} 

這表明數字格式異常請幫助我需要數不將字符串轉換爲整型

+2

這是不可讀的。 –

+0

你的問題是什麼? –

+1

你的空間有問題:** p =「」**將其改爲** p =「」** –

回答

-1
import java.util.*; 
class rev_sum { 
    public static void main(String args[]) 
    { 
     int a,i,n,m=0,t=0; 
     char c; 
     String w,p=" "; 
     Integer intCost=0 ; 
     Scanner sc=new Scanner(System.in); 
     System.out.println("Enter the number"); 
     n=sc.nextInt(); 
     w=Integer.toString(n); 
     System.out.println(w); 
    } 
} 

我檢查了它,現在它是確定的總和。

0

你有空間的問題:

代碼顯示此錯誤:

Enter the number 
3 
Exception java.lang.NumberFormatException: For input string: "3 " 

所以要解決這個問題,你只需要刪除P的這個空間:

String w, p = " "; 

更改此項

p = " " with p = ""; 
+1

我能理解你爲什麼做了downvote? –