2017-08-10 139 views
-9

在我QuestionnaireUI.java類「字符串不能轉換爲整數」

private void btnProceedActionPerformed(java.awt.event.ActionEvent evt) {           
     tblResults.setRowSelectionAllowed(true); 
     int temp = tblResults.getSelectedRow(); 
     Global.manu = tblResults.getValueAt(temp, 1).toString(); 
     Global.mod = tblResults.getValueAt(temp, 2).toString(); 
     Global.price = "R" +  (Integer)tblResults.getValueAt(temp,3).toString(); //line of code that gives me the error message 

     this.dispose(); 
     new PaymentUI().setVisible(true); 
    } 

在,我用我所有的全局變量

public class Global { 

    public static int rowSelect; 
    public static String manu; 
    public static String mod; 
    public static int price; 
    public static int financeprice; 
    public static int rate = 9; 

} 
+0

後的Java 5'的ToString ()'是不需要的,因爲自動裝箱會將其轉換爲字符串... –

+4

如何將R'轉換爲int? – Jens

+0

BTW:屬性不應公開。你應該讓他們私密,並通過getter和setter訪問 – Jens

回答

0

要添加R(我Global.java類字符串)(Integer)tblResults.getValueAt(temp,3).toString()

這裏有幾個問題:

tblResults.getValueAt(temp,3).toString()返回一個字符串,它不是一個整數,因此不能轉換爲整數。

向任何東西添加一個字符串總會給你一個字符串,所以"R" + something將始終返回一個字符串。然後嘗試將其分配給Integer。

0

由於+運營商String作爲一個參數轉換其他參數String

Global.price = "R" + tblResults.getValueAt(temp,3); 

除非price聲明爲int在這種情況下:

Global.price = (Integer)tblResults.getValueAt(temp,3);