Search code examples
javastringvalidationintjtextfield

JTextField Data Validation


New to Java, so any help would be greatly appreciated. Have a small problem with data validation in a JTextField. A user is asked to enter their Age, Whether they smoke, and whether they are overweight. Validation for smoking and weight works fine, so does the age limit I have set.

However if I enter a letter in the ageField JTextField it seems to get stuck and doesn't print the other validation errors. (E.g. It will correctly print "Age must be an integer", however if I also typed 'h' in smokesField, "Smoke input should be Y, y, N or n" would not be printed.)

Sorry, that's a long and bloated explanation!

Anyway here is the code I'm having difficulties with, thankyou:

public void actionPerformed(ActionEvent e)
{
String ageBox = ageField.getText();
int age = 0;

if (e.getSource() == reportButton)
{
    if (ageBox.length() != 0)
        {
            try
            {
            age = Integer.parseInt(ageBox);
            }
            catch (NumberFormatException nfe)
            {
            log.append("\nError reports\n==========\n");    
            log.append("Age must be an Integer\n");
            ageField.requestFocus();
            }        
        }
    if (Integer.parseInt(ageBox) < 0 || Integer.parseInt(ageBox) > 116)
    {
      log.append("\nError reports\n==========\n");  
      log.append("Age must be in the range of 0-116\n");
      ageField.requestFocus();
    }
    if (!smokesField.getText().equalsIgnoreCase("Y") && !smokesField.getText().equalsIgnoreCase("N"))
    {
        log.append("\nError reports\n==========\n");
        log.append("Smoke input should be Y, y, N or n\n");
        smokesField.requestFocus();
    }
    if (!overweightField.getText().equalsIgnoreCase("Y") && !overweightField.getText().equalsIgnoreCase("N"))
    {
        log.append("\nError reports\n==========\n");
        log.append("Over Weight input should be Y, y, N or n\n");
        smokesField.requestFocus();
    }
    }

Solution

  • From the situation you described, it's likely that the line

        if (Integer.parseInt(ageBox) < 0 || Integer.parseInt(ageBox) > 116)
    {
    ...
    

    is throwing an unhandled NumberFormatException, since you've entered a letter in ageBox. You get the correct output of "Age must be an Integer" the first time since your exception is being caught by your try/catch handler, but this second occurrence has no such handling.

    To fix this, I would simply move that particular if statement inside the try block, like so:

        try
        {
            if (Integer.parseInt(ageBox) < 0 || Integer.parseInt(ageBox) > 116)
            {
                log.append("\nError reports\n==========\n");  
                log.append("Age must be in the range of 0-116\n");
                ageField.requestFocus();
            }
        }
        catch (NumberFormatException nfe)
        ...
    

    This way, you'll still get the output of "Age must be an Integer" if ageBox has an invalid entry, and everything else should run fine.