0
do {
    number = (String)JOptionPane.showInputDialog(null,"Quantity : ","TRIAL",JOptionPane.QUESTION_MESSAGE);
    if(number.matches("\\d+")) {
        qty = Integer.parseInt(number);
    }
    //    JOptionPane.showMessageDialog(null,"Invalid input !\n\nMin = 1\nMax = 100","TRIAL",JOptionPane.ERROR_MESSAGE);
} while(qty < 1 || qty > 100);
JOptionPane.showMessageDialog(null,number);

if I put the error message inside or outside if, the error message still appear if user key-in right data

3 Answers 3

1

You want to show the message if the input doesn't match the regular expression. So this:

if(number.matches("\\d+")){
    qty = Integer.parseInt(number);
}
//    JOptionPane.showMessageDialog(null,"Invalid input !\n\nMin = 1\nMax = 100","TRIAL",JOptionPane.ERROR_MESSAGE);

needs to become this:

if(number.matches("\\d+")){
    qty = Integer.parseInt(number);
} else {
    JOptionPane.showMessageDialog(null,"Invalid input !\n\nMin = 1\nMax = 100","TRIAL",JOptionPane.ERROR_MESSAGE);
}

Now you're saying that if it matches OK then you read the new value of qty, but if it doesn't then you show the message.

You might think about an alternative, which is to avoid the regular expression, but just attempt the parseInt() call, and then catch the NumberFormatException that results if it can't be converted.

(You also need some logic to show an error if the number can be parsed, but gives a result outside the valid range of 1 to 100.)

Sign up to request clarification or add additional context in comments.

1 Comment

The error message will appear if the user left it empty and put string only , but if user input the number outside the valid range it don't show the error message . @chiastic-security
1

You can move a condition check into the loop and change your loop to infinite:

while (true) {
    number = (String)JOptionPane.showInputDialog(null,"Quantity : ","TRIAL",JOptionPane.QUESTION_MESSAGE);
    if(number.matches("[1-9]\\d*")){
        qty = Integer.parseInt(number);

        if (qty >= 1 && qty <= 100) {
             break;
        }
    }

    JOptionPane.showMessageDialog(null, "Invalid input !\n\nMin = 1\nMax = 100", "TRIAL", JOptionPane.ERROR_MESSAGE);
}

JOptionPane.showMessageDialog(null,number);

In that case, you have an infinite loop, which will break only if the entered value contains only digits and it's value is within the range [1,100]

6 Comments

thanx u , its work . one more thing, how do I make number 01-09 invalid @Stanislav
@ZaemShakkir you can change the regex pattern to [1-9]\d*. it will accept only numbers with 1-9 at the begining
is it possible if I use the same code as you suggest to validate string , sory I'm still newbie in this java . hope u don't feel burden in helping me :) @Stanislav
@ZaemShakkir I didn't properly understand your question) you are free to use this code, sure, if it passes to your requirements
I mean if I want to validate textfield for string if user input numeric and left it empty by using the code given. is it possible ? @Stanislav
|
1

How do I call error message if user input wrong numeric data

I would instead suggest offering them a control which makes selecting a number, easy. Specifically, a spinner with a spinner number model. Like this:

import javax.swing.*;

public class PickANumber {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                SpinnerNumberModel spinnerModel = 
                        new SpinnerNumberModel(0, 0, 100, 1);
                JSpinner spinner = new JSpinner(spinnerModel);
                while (spinnerModel.getNumber().intValue()<1) {
                    JOptionPane.showMessageDialog(
                            null, 
                            spinner, 
                            "Pick a number between 1 & 100!", 
                            JOptionPane.QUESTION_MESSAGE);
                }
                System.out.println(
                        "Number: " + spinnerModel.getNumber().intValue());
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

1 Comment

Thanx u for your suggestion but I couldn't understand the code because in my college I only learn basic java which is only using JOptionPane . The rest I only learned it from the internet . :) thank u once again @Andrew Thompson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.