Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why doesnt my program work? (I am a java newbie)

1 view
Skip to first unread message

Jen Hale

unread,
Oct 22, 2001, 12:30:11 AM10/22/01
to
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...

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

unread,
Oct 22, 2001, 12:24:32 AM10/22/01
to
Strings are immutable. you can't change them. try changing retValue
from a String to a StringBuffer

-krateo

Kent

unread,
Oct 22, 2001, 2:21:09 AM10/22/01
to

"Jen Hale" <isa...@digitalriver.com> wrote in message
news:3bd3...@MAIL.mhogaming.com...

^ delete ";"

> if (ch!='\n') retValue += (char) ch;

^^^ Change '\n' to 13


> return retValue;
> }
> }
>
>
>
>
>
>


Joachim Sauer

unread,
Oct 22, 2001, 3:30:42 AM10/22/01
to
"Jen Hale" <isa...@digitalriver.com> wrote:

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

Paul Lutus

unread,
Oct 22, 2001, 3:55:00 AM10/22/01
to
"Kent" <hong...@etang.com> wrote in message
news:9r0dou$d6a$1...@mail.cn99.com...

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


TGOS

unread,
Oct 22, 2001, 1:22:09 PM10/22/01
to
=< Quoted Text Info >=
|- from: "Jen Hale" <isa...@digitalriver.com>
|- to: comp.lang.java.help
|- on: Sun, 21 Oct 2001 21:30:11 -0700


> 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

Paul Lutus

unread,
Oct 22, 2001, 3:31:57 PM10/22/01
to
"TGOS" <tg...@spamcop.net> wrote in message
news:llk8ttkpe2l9m7kub...@4ax.com...

> =< Quoted Text Info >=
> |- from: "Jen Hale" <isa...@digitalriver.com>
> |- to: comp.lang.java.help
> |- on: Sun, 21 Oct 2001 21:30:11 -0700
>
>
> > 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.
>

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


Paul Lutus

unread,
Nov 2, 2001, 7:26:49 PM11/2/01
to
"TGOS" <tg...@spamcop.net> wrote in message
news:jkb6utkjml090g4pr...@4ax.com...

> =< Quoted Text Info >=
> |- from: Clyde <use...@mcenter.com>
> |- to: comp.lang.java.help
> |- on: Fri, 02 Nov 2001 18:23:58 -0500
>
>
> > You probably meant something like this:

> >
> >
> > 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;
> > }
>
> This is looks awful to me.

>
> public static String getString() throws IOException {
> StringBuffer retValue = new StringBuffer(10);

> int ch;
>
> System.out.println("Please enter a number :");
> ch = System.in.read();
> while (ch != -1 && (char)ch != '\n') {
> retValue.append((char)ch);
> ch = System.in.read();
> }
> return retValue.toString();
> }

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


0 new messages