# A non-integer value

Hi every one..

The question is>>A program is required to read three numbers,
multiply them together, and print the result. Write the program so
it stops when the user enters a non-integer (that is, the program
continues forever until the user stops it) and use 2 methods/functions:
one to prompt user for input and one to read console.

And my question is how will the program know that a non integer value has been entered

###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Let's say you have the user input the number like so:

String input = br.readLine();
int intValue;
try {
intValue = Integer.parseInt(input);
}
catch(NumberFormatException e) {
System.out.println("the number wasnt an integer value");
}
0
Author Commented:

thanks very much..but can you simplify it to me more please..Im just a beginner..

I will use this simple method to get input..

String num1 = JOptionPane.showInputDialog("Enter the first number")..

what do I have to do next?
0
Commented:
try {
intValue = Integer.parseInt(num1);
}
catch(NumberFormatException e) {
System.out.println("the number wasnt an integer value");
}

Basically, the line:
intValue = Integer.parseInt(num1);

tries to convert the string num1 to an integer value and assign it to intValue.  If num1 represents an integer ("232" for example), then the command will work fine.  If the string contains a non-integer though ("asdf" for example), then the call with throw a NumberFormatException in the catch block.
0
Author Commented:

that helped me very much..

but can you show me how can I use it inside the loop and for three numbers..?
because the program will keep asking the user to input three numbers until the user enter a non negative number..
0
Author Commented:
Sorry..I mean until the user enters a non-integer value
0
Commented:
String num1;
int intValue;
boolean loop = true;
while(loop) {
JOptionPane.showInputDialog("Enter the first number")..
try {
intValue = Integer.parseInt(num1);
}
catch(NumberFormatException e) {
System.out.println("the number wasnt an integer value");
loop = false; // set loop to false so we exit the while loop
}
}// end while
0
Author Commented:

Sorry..but I still can't get it..

In my program I want to ask the user to enter the first number..
and then the second number and then the third..
but I only have one window to enter the first number..

also I will have to multiply the three numbers and show result before proceed..

could you please explain more how can I do this..
0
Commented:
Just ask the way you do for the first number, except do it 3 times.
String num;
int intValue1;
int intValue2;
int intValue3;
boolean loop = true;
while(loop) {

try {
num = JOptionPane.showInputDialog("Enter the first number")..
intValue1 = Integer.parseInt(num);
num = JOptionPane.showInputDialog("Enter the second number")..
intValue2 = Integer.parseInt(num);
num = JOptionPane.showInputDialog("Enter the third number")..
intValue3 = Integer.parseInt(num);

// now you can multiply the three numbers together or do whatever you want here.
}
catch(NumberFormatException e) {
System.out.println("the number wasnt an integer value");
loop = false; // set loop to false so we exit the while loop
}
}// end while
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:

thanks very much :)

solution accepted
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.