?
Solved

A non-integer value

Posted on 2003-11-14
9
Medium Priority
?
431 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

764 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