我正在构建一个客户端/服务器应用程序,我需要您帮助解决以下问题。
我有一个 JTable
,如下图所示:
我想为每个客户都买一瓶,bottle=("Flacon")
应该有:
每瓶最多 9.9 毫升。
每瓶最多 5 剂。
这是我尝试过的代码:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
int dose=0;
Double summl =0.0;
String Name="";
Vector vf = null;
Vector<Vector> data = new Vector<Vector>();
for(int i=0; i <jTable1.getRowCount(); i++)
{
try {
if(jTable1.getValueAt(i, 1).toString().equals(jTable1.getValueAt(i+1, 1).toString()))
{
dose++;
summl=summl+(Double)nf.parse(jTable1.getValueAt(i,5).toString());
Name=jTable1.getValueAt(i, 1).toString();
}else
{
vf=new Vector();
vf.add(countflac);
vf.add(Name);
vf.add(summl);
data.add(vf);
countflac++;
}
} catch (ParseException ex) {
ex.printStackTrace();
}
}
jTable3.setModel(new DefaultTableModel( data,vflac));
System.out.println("Sum of ml's ="+summl);
System.out.println("dose ="+dose);
}
它给出了以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
最佳答案
getValueAt(i+1, 1)
最后一行出错,因为 i+1 必须小于 getRowCount()。
关于java - 如何将 double 分成单独的收集值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8355709/