0
votes

The code is supposed to get an order and its price and save them in their respective ArrayLists.

public class SetMenu0{

    private double price;
    private int size;
    private String output;
    private String priceOutput;
    String next;
    JTextField orderIn;
    JTextField priceIn;

    private JFrame orderInput;

    JPanel txtFldPanel;
    JPanel btnPanel;

    ArrayList orderList = new ArrayList<String>();
    ArrayList priceList = new ArrayList<Double>();

    public SetMenu0()
    {
        orderInput = new JFrame();
        orderInput.setTitle("Input Order and Price");
        orderInput.setSize(200,350);
        orderInput.getContentPane();
        orderInput.setLayout(new BorderLayout());
        orderInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel txtFldPanel = new JPanel();
        orderIn = new JTextField(10);
        priceIn = new JTextField(5);
        txtFldPanel.add(orderIn);
        txtFldPanel.add(priceIn);

        JPanel btnPanel = new JPanel();
        JButton addBtn = new JButton("ADD");
        btnPanel.add(addBtn);
        addBtn.addActionListener(new ButtonListener());
        addBtn.setActionCommand("add");
        JButton fnshBtn = new JButton("FINISH");
        btnPanel.add(fnshBtn);
        addBtn.addActionListener(new ButtonListener());
        fnshBtn.setActionCommand("fnsh");

        size = orderList.size();

        Container cont = orderInput.getContentPane();
        cont.add(txtFldPanel,BorderLayout.NORTH);
        cont.add(btnPanel,BorderLayout.SOUTH);

        orderInput.pack();
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String actionEnter = e.getActionCommand();
            if(actionEnter.equals("add"))
            {
                orderList.add(orderIn.getText());
                priceList.add(Double.parseDouble(priceIn.getText()));
                orderIn.setText("");
                priceIn.setText("");
            }
            else if(actionEnter.equals("fnsh"))
            {
                orderInput.dispose();
            }
        }
    }

    public JFrame getFrame()
    {
        return orderInput;
    }

    public ArrayList getOrd()
    {
        return orderList;
    }

    public ArrayList getPri()
    {
        return priceList;
    }

    public int getSize()
    {
        return size;
    }

}

When i press the ADD button it shows a NumberFormatException. Why is this? I could add the rest of the code from the main but why is it not saving in the ArrayList?

This is the error: java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.howtodoinjava.demo.poi.SetMenu0$ButtonListener.actionPerformed(SetMenu0.java:84) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

2
The exception should show you which line caused the problem. - keuleJ
Sorry that's all i got - user3752231
it doesn't. just takes me to a window that says java exception breakpoint - user3752231
Your problem is here com.howtodoinjava.demo.poi.SetMenu0$ButtonListener.actionPerformed(SetMenu0.java:84 You are trying to parse an empty String - keuleJ
yeah, thanks. i fixed i by changing the int output to "0" - user3752231

2 Answers

1
votes

It's probably due to this line:

priceList.add(Double.parseDouble(priceIn.getText()));

You want to make sure that priceIn text field contains a number before you try to parse the text.

0
votes

You have to use (on button add click)

if(!(priceIn.isEmpty || orderIn.isEmpty)){
     //Do your adding
}

This code checks if there is something in the JTextFiels