【发布时间】:2018-01-18 20:44:58
【问题描述】:
在我的 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);
}
在我用于所有全局变量的 Global.java 类中
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;
}
【问题讨论】:
-
在 Java 5 之后
.toString()不需要,因为自动装箱会将其转换为String... -
如何将
R转换为int? -
顺便说一句:属性不应该是公开的。您应该将它们设为私有并通过 getter 和 setter 进行访问
-
@UsagiMiyamoto 自动装箱不会自动将整数转换为字符串;它只是自动将原始类型(例如
int)装箱到其对应的包装对象java.lang.Integer中。