Please enter a number :
45
Exception in thread "main" java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java:435)
at java.lang.Integer.parseInt(Integer.java:463)
at MaxandMin.main(MaxandMin.java:17)
Press any key to continue...
Could some please help me out on how to fix this exceptional error. If
possible can you change the program and explain it to me how you fixed it.
Thanks.
----------------------------------------------------------------------------
-----------------------------
/* Program: Asks the user the to input number.
Input: enter Integers.
Output: Outputs the Minumum and Maximum number
*/
import java.io.*;
public class MaxandMin{
public static void main(String arg[]) throws Exception
{
int numb;
int maxNumb;
int minNumb;
String numbword = getString( );
numb = Integer.parseInt(numbword);
maxNumb = numb;
minNumb = numb;
while ( numb > -1)
{
if (maxNumb <= numb)
maxNumb = numb;
if (minNumb > numb)
minNumb = numb;
numb = Integer.parseInt((getString( )));
}
System.out.println("The min number entered was " + minNumb);
System.out.println("The max number entered was " + maxNumb);
}
public static String getString( ) throws Exception
{
String retValue = "";
int ch;
System.out.println("Please enter a number :");
while
( ((ch = System.in.read()) != -1) && ((char) ch != '\n'));
if (ch!='\n') retValue += (char) ch;
return retValue;
}
}
-krateo
^ delete ";"
> if (ch!='\n') retValue += (char) ch;
^^^ Change '\n' to 13
> return retValue;
> }
> }
>
>
>
>
>
>
> When I try to run the program below it gives me a Exception error. Why?
> The program compiles fine. The error I recieve is
>
>
>
> Please enter a number :
> 45
> Exception in thread "main" java.lang.NumberFormatException:
> at java.lang.Integer.parseInt(Integer.java:435)
> at java.lang.Integer.parseInt(Integer.java:463)
> at MaxandMin.main(MaxandMin.java:17)
> Press any key to continue...
>
Contrary to popular belief Exceptions and other Error messages have an
reason for existence, other than just anoying the programmer/user. If you
look especially hard, you will notice that the Exception thrown in this
Example is a NumberFormatException. The name alone might allready give you
an idea what went wrong.
Lets assume it doesnt: You wonder why this Exception is thrown. There are
two ways to find out:
1.) Look in the Java API-Docs at java.lang.NumberFormatException
or
2.) Look in the Java API-Docs at the Function that throws the Exception
(here java.lang.Integer.parseInt)
(You don't have the Java API-Docs? Go get them:
http://java.sun.com/j2se/1.3/docs.html)
Both ways will tell you that this Exception is thrown, when the String
passed to parseInt doesn't contain a valid number ... as I see from your
posting you seem to have entered a valid number, so the problem seems to be
in the conversion code ...
Maybe you'd like to print the String you pass to the parseInt-Function
before doing so, just to check wether it is valid ...
good luck
Joachim Sauer
--
Das legt dann nochmal eben eine halbe Mio Bananen an, jede ca. 24 Byte
groß. Macht dann nochmal ca. 12 MB.
[angewandte OOP in de.comp.lang.java]
> > if (ch!='\n') retValue += (char) ch;
> ^^^ Change '\n' to 13
No, no, no. Never use a number to represent a character for which a symbol
exists, and all common characters have symbols. This is a great way to make
a program fail in mysterious ways when the platform changes. Even better
would be to look at System.properties to see what the system's EOL character
is. But don't just type in some number you think might represent a
character. It might be true for a week, then false for a week. It might be
true in one place and false in another. This practice breaks portability
like nothing else I have even seen.
The OP should simply catch the exception. He could also use a simpler input
filter:
while((ch = System.in.read()) >= ' ') {
// user code
}
--
Paul Lutus
www.arachnoid.com
> public static String getString( ) throws Exception
> {
>
> String retValue = "";
>
> int ch;
>
> System.out.println("Please enter a number :");
> while
> ( ((ch = System.in.read()) != -1) && ((char) ch != '\n'));
> if (ch!='\n') retValue += (char) ch;
> return retValue;
> }
>}
I assume your problem is here. In Java it's not possible to alter
Strings.
Try:
public static String getString() throws IOException {
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Please enter a number:");
return in.readLine();
}
And then you should catch the NumberFormatException, or your program
will crash whenever somebody enters something that is not a number
(regardless what Paul Lutus says).
Something like:
numb = -100;
while (numb == -100) {
try {
numb = Integer.parseInt(numbword);
} catch (NumberFormatException e) {
System.out.println("What you entered was not a valid number.");
System.out.println("Please try again:");
}
}
I used -100, because -1 is already used to end your program.
--
TGOS
No, that is not the problem. Any single String object is immutable, but Java
deals with this (inefficiently) by replacing one String object with another
when a concatenation is required. The code is not efficient, but it is also
not the cause of the problem.
--
Paul Lutus
www.arachnoid.com
This looks awful to me. :) You have two lines that do the same thing, a size
inefficiency and a comprehension and maintenance issue. Whenever possible,
do something just once.
Your BufferedReader was a good suggestion, but for those cases that require
character-by-character reading:
int c;
while((c = System.in.read()) != -1 && ((char)c != '\n')) {
retValue.append((char)ch);
}
The only real problem with the OP's code was that he tested for '\n' twice,
and only one of them was needed.
> System.out.println("Please enter a number :");
> return in.readLine();
Hmm. Don't you want the prompt on the same line as the entry?
System.out.print("Please enter a number :");
return in.readLine();
--
Paul Lutus
www.arachnoid.com