Skip to content
Advertisement

How check radio buttons checked or not?

I have tried simple java programme using if statements.
for that, I’m using radio buttons to check whether is check or not?
please check my code below is not working properly someone please help me on this?
Project link below
https://drive.google.com/file/d/1xhNbKyXYJh2k5i7a6nVl1VkTY7dTNzSg/view?usp=sharing

private void BTN_submitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String finalResult;
        int age = Integer.parseInt(TXT_age.getText());
        RBTN_male.setActionCommand("male");
        RBTN_female.setActionCommand("female");
        String radio_Result = BTNgrp_gender.getSelection().getActionCommand();


        if (RBTN_male.isSelected() || RBTN_female.isSelected()) {
            if (TXT_age.getText() == null || TXT_age.getText().trim().isEmpty()) {

                JOptionPane optionPane = new JOptionPane("Please fill your age", JOptionPane.ERROR_MESSAGE);
                JDialog dialog = optionPane.createDialog("Age missing");
                dialog.setAlwaysOnTop(true);
                dialog.setVisible(true);

            } else {
                if (age == 0) {
                    JOptionPane optionPane = new JOptionPane("Please enter valid age", JOptionPane.ERROR_MESSAGE);
                    JDialog dialog = optionPane.createDialog("Wrong age");
                    dialog.setAlwaysOnTop(true);
                    dialog.setVisible(true);
                } else {
                    if (age > 0 && age <= 5) {
                        finalResult = "Rhymes";
                        LBL_result.setText(finalResult);
                    } else if (age > 15) {
                        finalResult = "Poetry";
                        LBL_result.setText(finalResult);
                    }
                }
            }

        } else {
            JOptionPane optionPane = new JOptionPane("Please Select Your Gender", JOptionPane.ERROR_MESSAGE);
            JDialog dialog = optionPane.createDialog("Gender Missing");
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);
        }


    }                                          

Advertisement

Answer

The main problem with your code is in the second line of your method BTN_submitActionPerformed

int age = Integer.parseInt(TXT_age.getText()); 

Here the value for TXT_age is “Enter your age”. Now this cannot be parsed to an integer therefore a NumberFormatException is thrown which prevents the program from continuing its expected course of execution. The program also removes the placeholder text from this field on click leaving it empty i.e “” therefore submitting it will also result in an error.

To solve the issues above you can rewrite this method as follows:

    private void BTN_submitActionPerformed(java.awt.event.ActionEvent evt) {
        int age;
        String finalResult;
        JOptionPane optionPane;
        JDialog dialog;
        RBTN_male.setActionCommand("male");
        RBTN_female.setActionCommand("female");
        try {
            age = Integer.parseInt(TXT_age.getText());
            if (RBTN_male.isSelected() || RBTN_female.isSelected()) {

                if (age == 0  || age < 0 ) {
                    optionPane = new JOptionPane("Please enter valid age", JOptionPane.ERROR_MESSAGE);
                    dialog = optionPane.createDialog("Wrong age");
                    dialog.setAlwaysOnTop(true);
                    dialog.setVisible(true);
                } else {
                    if (age > 0 && age <= 5) {
                        finalResult = "Rhymes";
                        LBL_result.setText(finalResult);
                    } else if (age > 15) {
                        finalResult = "Poetry";
                        LBL_result.setText(finalResult);
                    }
                }

            } else {
                optionPane = new JOptionPane("Please Select Your Gender", JOptionPane.ERROR_MESSAGE);
                dialog = optionPane.createDialog("Gender Missing");
                dialog.setAlwaysOnTop(true);
                dialog.setVisible(true);
            }
        } catch (NumberFormatException e) {
            optionPane = new JOptionPane("Please fill your age", JOptionPane.ERROR_MESSAGE);
            dialog = optionPane.createDialog("Age missing");
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);
        }

    }

On a sidenote you can also think of other problems that may arise. For example, as user can enter anything in the textfield that means a user can enter a number that may not fit in an int. So if that happens then your program will again break. However, if you make above mentioned changes to your method then the problems that you are facing currently will be resolved.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement