Solved

A non-integer value

Posted on 2003-11-14
9
426 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 67
unix in java example 9 52
java 8 lambda expresssions exception handling 3 72
Java - Why doesn't this JFrame work 3 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

930 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

11 Experts available now in Live!

Get 1:1 Help Now