?
Solved

A non-integer value

Posted on 2003-11-14
9
Medium Priority
?
433 Views
Last Modified: 2010-03-31

Hi every one..

would you please help me with my question..

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

thanks in advance,,
0
Comment
Question by:SEstudent
  • 5
  • 4
9 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9753316
Let's say you have the user input the number like so:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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 Comment

by:SEstudent
ID: 9753340

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
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9753374
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:SEstudent
ID: 9753385

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 Comment

by:SEstudent
ID: 9753395
Sorry..I mean until the user enters a non-integer value
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9753404
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 Comment

by:SEstudent
ID: 9753413

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
 
LVL 5

Accepted Solution

by:
lwinkenb earned 200 total points
ID: 9753443
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
 

Author Comment

by:SEstudent
ID: 9753486

thanks very much :)

solution accepted
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question