Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating to ensure user selects an integer.

Status
Not open for further replies.

lilal111

Technical User
Sep 16, 2008
18
0
0
CA
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){
productCounter++;
quantity=Integer.parseInt(input);

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
 
A way to proceed could be

Code:
try {
    quantity=Integer.parseInt(input);
} catch (NumberFormatException nfe) {
       //whatever you want to do to show the error
}

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

Thanks

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

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top