NumberFormatException: For input string: "Regular (Php 100)"

429 Views Asked by At

The program is supposed to compute the integer indicated for the radio button, checkbox, and combo box, but it always returns an error when I submit it.

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Regular (Php 100)"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:668)
    at java.base/java.lang.Integer.parseInt(Integer.java:786)
    at Marte_project$5.actionPerformed(Marte_project.java:157)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)

public Marte_project() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 665, 703);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    JLabel lblNewLabel = new JLabel("Marte's Pizza Project");
    lblNewLabel.setFont(new Font("Black Clover Font", Font.BOLD, 60));
    lblNewLabel.setBounds(80, 36, 486, 60);
    contentPane.add(lblNewLabel);
    
    JLabel lblNewLabel_1 = new JLabel("Order");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblNewLabel_1.setBounds(161, 118, 51, 25);
    contentPane.add(lblNewLabel_1);
    
    JPanel panel = new JPanel();
    panel.setBounds(193, 152, 234, 169);
    contentPane.add(panel);
    panel.setLayout(null);
    
    //start radio button
    JRadioButton rdb_regular = new JRadioButton("Regular (Php 100)");
    rdb_regular.setSelected(true);
    JRadioButton rdb_special = new JRadioButton("Special (Php 120)");
    JRadioButton rdb_super = new JRadioButton("Super Special (200)");
    
    rdb_regular.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdb_regular.isSelected()) {
                rdb_special.setSelected(false);
                rdb_super.setSelected(false);
            }
        }
    });
    rdb_regular.setFont(new Font("Tahoma", Font.PLAIN, 20));
    rdb_regular.setBounds(25, 18, 187, 33);
    panel.add(rdb_regular);
    
    
    rdb_special.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdb_special.isSelected()) {
                rdb_regular.setSelected(false);
                rdb_super.setSelected(false);
            }
        }
    });
    rdb_special.setFont(new Font("Tahoma", Font.PLAIN, 20));
    rdb_special.setBounds(25, 68, 181, 33);
    panel.add(rdb_special);
    
    
    rdb_super.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rdb_super.isSelected()) {
                rdb_regular.setSelected(false);
                rdb_special.setSelected(false);
            }
        }
    });
    rdb_super.setFont(new Font("Tahoma", Font.PLAIN, 20));
    rdb_super.setBounds(25, 114, 199, 33);
    panel.add(rdb_super);
    
    JLabel lblNewLabel_2 = new JLabel("Add-ons");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblNewLabel_2.setBounds(161, 340, 73, 25);
    contentPane.add(lblNewLabel_2);
    
    JCheckBox ckb_cheese = new JCheckBox("Cheese (+Php 10)");
    ckb_cheese.setFont(new Font("Tahoma", Font.PLAIN, 20));
    ckb_cheese.setBounds(214, 372, 187, 33);
    contentPane.add(ckb_cheese);
    
    JCheckBox ckb_bacon = new JCheckBox("Bacon (+Php 15)");
    ckb_bacon.setFont(new Font("Tahoma", Font.PLAIN, 20));
    ckb_bacon.setBounds(214, 408, 177, 33);
    contentPane.add(ckb_bacon);
    
    JCheckBox ckb_pepperoni = new JCheckBox("Pepperoni (+Php 20)");
    ckb_pepperoni.setFont(new Font("Tahoma", Font.PLAIN, 20));
    ckb_pepperoni.setBounds(214, 444, 211, 33);
    contentPane.add(ckb_pepperoni);
    
    JLabel lblNewLabel_3 = new JLabel("Membership");
    lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblNewLabel_3.setBounds(161, 495, 108, 25);
    contentPane.add(lblNewLabel_3);
    
    JComboBox cb_member = new JComboBox();
    cb_member.setFont(new Font("Tahoma", Font.PLAIN, 20));
    cb_member.setModel(new DefaultComboBoxModel(new String[] {"Yes", "No"}));
    cb_member.setBounds(300, 492, 133, 31);
    contentPane.add(cb_member);
    
    JLabel lblNewLabel_4 = new JLabel("Quantity");
    lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblNewLabel_4.setBounds(137, 541, 75, 25);
    contentPane.add(lblNewLabel_4);
    
    txt_qtty = new JTextField();
    txt_qtty.setBounds(255, 544, 207, 26);
    contentPane.add(txt_qtty);
    txt_qtty.setColumns(10);
    
    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int cheese, bacon, pepperoni, quantity,result,price, discount;
            String total = "";
            price = Integer.parseInt(rdb_regular.getText());
            price = Integer.parseInt(rdb_special.getText());
            price = Integer.parseInt(rdb_super.getText());
            
            cheese = Integer.parseInt(ckb_cheese.getText());
            bacon = Integer.parseInt(ckb_bacon.getText());
            pepperoni = Integer.parseInt(ckb_pepperoni.getText());
            discount = Integer.parseInt((String)cb_member.getSelectedItem());
            
            if(rdb_regular.isSelected()) {
                price = 100;
            }
            else if (rdb_special.isSelected()) {    
                price = 120;
            }
            else {
                price = 200;
            }
            
            if(ckb_cheese.isSelected()) {
                cheese = 10;
            }
            if (ckb_bacon.isSelected()) {
                bacon = 15;
            }
            if (ckb_pepperoni.isSelected()){
                pepperoni = 20;
            }
            
            cb_member.getSelectedItem();
            if(cb_member.getSelectedItem().toString().equals("Yes")) {          
                discount = -5;
            }
            
            quantity = Integer.parseInt(txt_qtty.getText());
            result = price + cheese + bacon + pepperoni + discount;
            total = "The total amount is " + result * quantity; 
            
            JOptionPane.showMessageDialog(null, total);
        }
        
    });
    submit.setFont(new Font("Tahoma", Font.PLAIN, 20));
    submit.setBounds(251, 590, 140, 40);
    contentPane.add(submit);
}
}
1

There are 1 best solutions below

0
Jay On

You need to pass a valid number. Else you would get such NumberFormatException.

Regular (Php 100) is not a valid number.