gpt4 book ai didi

java - 如何解决 java.lang.ClassCastException 错误

转载 作者:行者123 更新时间:2023-11-29 04:31:10 27 4
gpt4 key购买 nike

我在 swing eclipse 的 JTable 属性中设计了默认表模型。所以当我尝试进行计算时出现错误......线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:java.lang.String无法转换为java.lang.Integer

public class punchingPBill extends JFrame
{
DefaultTableModel model;
Double rate,discount,amount,exciseduty,outputvat;

{
String[] columnNames = {"Sr.No", "Description Of Goods", "quantity", "Rate", "Per", "Discount", "Basic Excise Duty", "Output Vat", "Amount"};
Object[][] data = { {new Integer(1),"",new Integer(1), new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0),new Double(0.0)}};

model = new DefaultTableModel(data, columnNames);
}
table.setModel(model);
table.setRowHeight(25);

private void setTableModelListener()
{
System.out.println("enter listen");
TableModelListener tableModelListener = new TableModelListener()
{
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
System.out.println("Cell " + e.getFirstRow() + ", "
+ e.getColumn() + " changed. The new value: "
+ table.getModel().getValueAt(e.getFirstRow(),
e.getColumn()));
int row = e.getFirstRow();
int column = e.getColumn();

if(column == 2||column == 3||column == 5)
{
Integer quantity = ((Integer) model.getValueAt(row, 2)).intValue();
rate = ((Double) model.getValueAt(row, 3)).doubleValue();
discount=((Double) model.getValueAt(row, 5)).doubleValue();
amount=new Double(rate-(quantity*rate*(discount/100)));

model.setValueAt(amount, row, 8);

}

}
}
};
table.getModel().addTableModelListener(tableModelListener);
}

在 - 整数数量 = ((Integer) model.getValueAt(row, 2)).intValue();......rate = ((Double) model.getValueAt(row, 3)) 处出现错误.doubleValue();......discount=((Double) model.getValueAt(row, 5)).doubleValue();

最佳答案

您可以使用 Integer.parseInt("StringValue") 代替强制转换,其中“StringValue”应该是可以转换为整数的适当数值

关于java - 如何解决 java.lang.ClassCastException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com