Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Validating to ensure user selects an integer.

Validating to ensure user selects an integer.

Validating to ensure user selects an integer.

I am working on a project and am stuck on something. The project calculates sales commissions from a company. The user inputs the persons first name,last name and then it prompts to collect the amounts of items sold in a variable called input. I am stuck on how to validate to ensure the selection is numeric only. Any help would be greatly appreciated.

Here is the code below:

  public void  start(){
        FirstName=JOptionPane.showInputDialog("Enter sales person First Name :");
        LastName=JOptionPane.showInputDialog("Enter sales person Last Name :");
        input=JOptionPane.showInputDialog(null, "Enter number of items sold for product"  +" "+ productCounter +" :");
        while (productCounter <4){
       if (productCounter==1){
          tot1  = quantity * Item1Price;
       }else if (productCounter==2){
           tot2 =quantity*Item2Price;
        } else if (productCounter==3){
           tot3 =quantity*Item3Price;
        } else if (productCounter==4)
        tot4 =quantity*Item4Price;

        }//end while

RE: Validating to ensure user selects an integer.

A way to proceed could be


try {
} catch (NumberFormatException nfe) {
       //whatever you want to do to show the error


RE: Validating to ensure user selects an integer.

Where  would I institute this code? Would it be possible to place it in the while loop?



RE: Validating to ensure user selects an integer.

I'd put it outside the while loop, as quantity is just evaluated once.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close