2017-08-10 166 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);