Java - 模数 - 从一个表添加到另一个表

时间:2012-04-14 16:31:38

标签: java jtable row modulus

我有一个JTable,其中一列预先填充了30分钟的时间段(6.30-24.00)。

现在我有另一张桌子,里面有一个电影片目列表,其中包含一段电影持续时间的栏目(以分钟为单位 - 例如140分钟)。

现在我有一个按钮来执行此操作。我做了一段代码,有趣的是,有时是有效的,有时却没有(在我添加3-4个标题之后)。它根据数学方程式增加了时间段。它给了我:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "DRAMA"

这是代码:

btnAddProg.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        try {
                        int dur = Integer.parseInt(progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+1).toString()) / 30;
                        int durT = Integer.parseInt(progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+1).toString());
            if(durT % 30 != 0)
            {
                dur += 1;   
            }   

                for(int i = 0; i < dur; i++)
                {
                    String value = progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()).toString();
                                        String value2 = progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+2).toString();
                                        channel1DataTitle.set(chOneTable.getSelectedRow()+i, value);
                                        channel1DataGenre.set(chOneTable.getSelectedRow()+i, value2);
                }
                                        chOneTable.repaint();
                        } catch (IndexOutOfBoundsException f) {

                            JOptionPane.showMessageDialog(frame,
                             "Please select a row in the Channel table!",
                                "Channel row not selected",
                                 JOptionPane.PLAIN_MESSAGE);


                        }


                    }
                });

谁能告诉我什么是错的?

2 个答案:

答案 0 :(得分:0)

您正在尝试解析不会转换为数字的String。看起来问题是你正在处理用户选择的任何内容。您需要限制正在处理的数据来自表中的某些列,或者在尝试处理数据之前验证数据。

答案 1 :(得分:0)

当您单击正确的列时它会起作用,当您单击另一列时失败,不是吗?您具有应用于变量列的固定逻辑(解析持续时间编号)(取决于用户单击的确切列)。使用固定数字访问列,不要检查所选列索引。