Solved

A non-integer value

Posted on 2003-11-14
9
424 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 61
countX 22 70
solarwind tftp server 2 31
Java Loop 4 3
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now