Solved

A non-integer value

Posted on 2003-11-14
9
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 50 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb mdb examples 1 21
SequenceInputStream example 3 22
Selenium docs api java index 3 69
Java program running SQL query 5 50
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

730 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