字符串无法转换为java错误

字符串无法转换为java错误,java,string,button,integer,Java,String,Button,Integer,MyButton4不工作,输出显示错误 线程“AWT-EventQueue-0”中出现异常 java.lang.ClassCastException:java.lang.String不能在knapsacproject.Interface.getTableData(Interface.java:198)处转换为java.lang.Integer 它显示了这条线的错误 tableData[i]=(int)model.getValueAt(i,colIndex) 怎么了 public int[] ge

MyButton4不工作,输出显示错误

线程“AWT-EventQueue-0”中出现异常 java.lang.ClassCastException:java.lang.String不能在knapsacproject.Interface.getTableData(Interface.java:198)处转换为java.lang.Integer

它显示了这条线的错误

tableData[i]=(int)model.getValueAt(i,colIndex)

怎么了

public int[] getTableData(DefaultTableModel model, Integer colIndex) {
    int nRow = model.getRowCount();
    int[] tableData = new int [nRow];

    for(int i = 0; i< nRow; i++) {
        tableData[i] = (int) model.getValueAt (i,colIndex);
    }
    return tableData;
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
    int nRow = model.getRowCount();

    algorithm algo = new algorithm(getTableData(model, 1),getTableData(model, 2),
    Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()));
    int[] result = algo.getResult();

    for(int i = 0; i < nRow; i++) {
        if(result[i]==1)
            model.setValueAt("take", i , 3);
        else
            model.setValueAt("leave", i, 3);
    }
}
public int[]getTableData(DefaultTableModel模型,整数colIndex){
int nRow=model.getRowCount();
int[]tableData=新的int[nRow];
对于(int i=0;i
将字符串值赋给int数组时出现此错误

您需要执行
Integer.parseInt()
将字符串转换为int

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));

将字符串值赋给int数组时会出现此错误

您需要执行
Integer.parseInt()
将字符串转换为int

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));

您需要使用下面的代码将字符串解析为整数

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));

您需要使用下面的代码将字符串解析为整数

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));

您正试图将方法
getValueAt(i,colIndex)
返回的
String
值强制转换为
int
(这是不可能的),因此它基本上给了您一个
ClassCastException

您需要的是使用答案中提到的方法之一将该
字符串
值转换为
整数
值:

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));


您正试图将方法
getValueAt(i,colIndex)
返回的
String
值强制转换为
int
(这是不可能的),因此它基本上给了您一个
ClassCastException

您需要的是使用答案中提到的方法之一将该
字符串
值转换为
整数
值:

tableData[i] = Integer.parseInt(model.getValueAt (i,colIndex));


错误告诉您无法将字符串强制转换为int。请改为尝试integer.parsetInt。顺便说一句,你可以在谷歌上搜索这一个。请检查模型的返回类型。getValueAt(…)?它看起来像是返回字符串,您试图将其转换为整数
algorithm
应该是
algorithm
。谢谢,我在下面回答了这个错误,告诉您不能将字符串强制转换为int。请改用integer.parsetInt。顺便说一句,你可以在谷歌上搜索这一个。请检查模型的返回类型。getValueAt(…)?它看起来像是返回字符串,您试图将其转换为整数<代码>算法应该是
算法
。谢谢,我回答如下谢谢,我回答如下谢谢,我回答如下谢谢,我回答如下谢谢,我回答如下谢谢,我回答如下谢谢