2013-10-11 22 views
0

得到整數你好,我是android系統 很新,我試圖從一個EditText獲得整數值,但是當我解析字符串整數i得到了NumberFormatException異常。 請幫我擺脫這個錯誤。 在此先感謝。不能從EditText上

計劃是:

int day,month,year; 
    EditText expense,ammount; 
    String[] exp=new String[10]; 
    int[] amt=new int[10]; 
    int count=0; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final Calendar cal=Calendar.getInstance(); 
     day=cal.get(Calendar.DAY_OF_MONTH); 
     month=cal.get(Calendar.MONTH)+1; 
     year=cal.get(Calendar.YEAR); 


     final TextView txtdate=(TextView)findViewById(R.id.txtdate); 

     expense=(EditText)findViewById(R.id.exp); 
     ammount=(EditText)findViewById(R.id.amnt); 


     final Button add=(Button)findViewById(R.id.btnadd); 
     final Button cancel=(Button)findViewById(R.id.btncancel); 
     final Button done=(Button)findViewById(R.id.btndone); 



     txtdate.setText(day+"/"+month+"/"+year); 

     add.setOnClickListener(new OnClickListener() { 
      public void onClick(final View v) { 

      getval(); 
      } 

     }); 
     cancel.setOnClickListener(new OnClickListener() { 
      public void onClick(final View v) { 

       clean(); 



      } 

     }); 
     done.setOnClickListener(new OnClickListener() { 
      public void onClick(final View v) { 

       total(); 
       getval();  
       clean(); 

     final TextView tv=(TextView)findViewById(R.id.textView1); 
     tv.setText(Integer.toString(total())); 
      } 

      private int total() { 
       int total = 0; 

       // TODO Auto-generated method stub 
       for(int i=0;i<=count;i++) 
       { 

        total+=amt[i]; 

       } 
       return total; 

      } 

     }); 

    } 
    protected void clean() { 
     // TODO Auto-generated method stub 
     expense.setText(" "); 
     ammount.setText(" "); 

    } 
    protected void getval() { 
     // TODO Auto-generated method stub 
     final Editable e2=expense.getText(); 
     final Editable e1=ammount.getText(); 

     final int i=Integer.parseInt(e1.toString()); 
     amt[count]=i; 
     exp[count]=e2.toString(); 
     System.out.println(amt[count]); 
     System.out.println(exp[count]); 
     count++; 

    } 
} 

的例外是:

java.lang.NumberFormatException: unable to parse ' 600' as integer 
+0

什麼是E1''600''值或'600' –

回答

0

試試這個..

final int i=Integer.parseInt(e1.toString().trim()); 

bacause有一個空間之前,這個數字看' 600'這就是爲什麼這個錯誤。 。

希望這將有助於..

+0

這是工作。謝謝 – user2869524

+0

@ user2869524歡迎接受我的答案。 – Hariharan

0

使用此代碼

final int f = -1; // or other invalid value 
if (edittext.getText().toString().length() > 0) 
f = Integer.parseInt(edittext.getText().toString()); 
+0

再次獲取相同的錯誤。 – user2869524

+0

其工作user2869524 ..你如何給編輯文本輸入.. – harikrishnan

1

您的整數的空間。

在XML中添加以下屬性到您的EditText只允許輸入整數:

android:inputType="number" 
5

取下數第一開頭或結尾空格:

int inputNumber = Integer.parseInt(editText.getText().toString().trim()); 

或者,你可以刪除所有使用正則表達式的字符串中的非數字字符:

String cleanInput = editText.getText().toString().replaceAll("[^\\d]", ""); 
int inputNumber = Integer.parseInt(cleanInput); 

雖然如果非數字輸入字符是一個問題,您可能想要將EditText限制爲僅數字。見this question。它說,添加以下屬性到的EditText:

android:inputType="number" 
+0

感謝它的工作 – user2869524

0

使用此行的EditText XML佈局,

android:inputType="number"