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

String to int conversion problem

1 view
Skip to first unread message

Tony

unread,
Sep 23, 2001, 9:08:12 AM9/23/01
to
With the code below I'm trying to convert user input "(a-j)" to an
integer. However, I keep getting errors with code I think should
work. I looked at the posts here and the javadocs, but being a newbie
a solution isn't presenting itself given the small amount of Java I do
know. So I'm turning to you for your suggestions. Any help is very
much appreciated. Thanks. -Tony

<code snip>

public static void main(String args[]) throws IOException
{
//int xCoord;

System.out.print("Enter x-coordinate (a-j): ");
System.out.flush();
int xCoord = bsInput.readX();

System.out.println("array x-coord: " + xCoord);
}


class bsInput {

static InputStreamReader is = new InputStreamReader(System.in);
static BufferedReader br = new BufferedReader(is);

public static int readX() throws IOException
{
String input = br.readLine();
int i = Integer.parseInt(input);
return i;
}

<code snip>

It compiles, but I get this error when trying to run it:

Enter x-coordinate (a-j): d
Exception in thread "main" java.lang.NumberFormatException: d
at java.lang.Integer.parseInt(Integer.java:414)
at java.lang.Integer.parseInt(Integer.java:463)
at bsInput.readX(coord.java:42)
at coord.main(coord.java:15)

Paul Keeble

unread,
Sep 23, 2001, 9:27:00 AM9/23/01
to
Well if you entered the letter "a" into your program of course it will throw a
NumberFormatException.

Integer.parseInt(String) will only accept valid numbers such as 5, -1, 0 ,
123549879 etc it doesn't accept "a" as "a" is not a number!

If your doing co-ordinates based on "a-j" as you put it then you need a char not
an int.

Tony

unread,
Sep 23, 2001, 10:06:07 AM9/23/01
to
My overall goal is to have a user input coordinates from a grid (ex.
b,5). These coordinates would correspond to an array (ex. gb[2][6]).

What I've been taught (i'm a newbie in a java class) is that the "b"
is a String object and you need to "convert" it. So, I thought I
would be able to convert it to an int. But it sounds like I need to
first convert it to a char and then to an int. Is this correct? If
so, is there a "shortcut" or another completely different way to doing
what I want to accomplish?

@rigidsoftware.com C. Lamont Gilbert

unread,
Sep 23, 2001, 10:28:47 AM9/23/01
to

"Tony" <tth...@mn.rr.com> wrote in message
news:v9qrqtcahqqtb1rds...@4ax.com...

> My overall goal is to have a user input coordinates from a grid (ex.
> b,5). These coordinates would correspond to an array (ex. gb[2][6]).
>
> What I've been taught (i'm a newbie in a java class) is that the "b"
> is a String object and you need to "convert" it. So, I thought I
> would be able to convert it to an int. But it sounds like I need to
> first convert it to a char and then to an int. Is this correct? If
> so, is there a "shortcut" or another completely different way to doing
> what I want to accomplish?
>
>

int Func(String x)
{
char c = x.charAt(0);
return Character.getNumericValue(x.charAt(0));
}


you need to of course interpret the possible values of the return, like -1
as an error.

Tony

unread,
Sep 23, 2001, 10:49:11 AM9/23/01
to
On Sun, 23 Sep 2001 14:28:47 GMT, "C. Lamont Gilbert" <Lamont_Gilbert
@ RigidSoftware.com> wrote:

>
>int Func(String x)
>{
> char c = x.charAt(0);
> return Character.getNumericValue(x.charAt(0));
>}
>
>
>you need to of course interpret the possible values of the return, like -1
>as an error.
>

Thanks a lot. I plugged it in and it worked like I wanted it to.
I've been stuck on this problem for a while (about a day) so I really
appreciate the help. Thanks.

Jon A. Cruz

unread,
Sep 23, 2001, 1:00:37 PM9/23/01
to Tony
"C. Lamont Gilbert" wrote:

> int Func(String x)
> {
> char c = x.charAt(0);
> return Character.getNumericValue(x.charAt(0));
> }

I'd suggest something more like:

// Currently returns the index number (a gives 0),
// or some negative value if it's not a letter
int letterToIndex( String s )
{
int val = Character.toLowerCase( s.charAt(0) ) - 'a';

// change val to -1 if it's over the top limit.
if ( val >= 26 )
{
val = -1;
}

return val;
}

--
Jon A. Cruz
http://www.geocities.com/joncruz/action.html

Paul Keeble

unread,
Sep 23, 2001, 4:19:25 PM9/23/01
to
Rather than returning -1 for errors there really ought to be a runtimeException
thrown rather than return -1 which is more a c style thing.

Jon A. Cruz

unread,
Sep 23, 2001, 4:37:32 PM9/23/01
to
Paul Keeble wrote:

> Rather than returning -1 for errors there really ought to be a runtimeException
> thrown rather than return -1 which is more a c style thing.

True.
I was mainly trying to stay close to what Tony did.

Kerry Shetline

unread,
Sep 23, 2001, 5:35:09 PM9/23/01
to
"Paul Keeble" <P.Ke...@ntlworld.com> wrote in message
news:Bolr7.5651$%a4.15...@news2-win.server.ntlworld.com...

> Well if you entered the letter "a" into your program of course it will
throw a
> NumberFormatException.
>
> Integer.parseInt(String) will only accept valid numbers such as 5, -1, 0 ,
> 123549879 etc it doesn't accept "a" as "a" is not a number!

Well... you could do Integer.parseInt(input, 20). By specifying a radix of
20, the letters A through J do indeed become numbers, covering the range
10-19 :)

-Kerry


jim korman

unread,
Sep 23, 2001, 11:55:09 PM9/23/01
to
Tony wrote:

> With the code below I'm trying to convert user input "(a-j)" to an
> integer. However, I keep getting errors with code I think should
> work. I looked at the posts here and the javadocs, but being a newbie
> a solution isn't presenting itself given the small amount of Java I do
> know. So I'm turning to you for your suggestions. Any help is very
> much appreciated. Thanks. -Tony
>

> <snipped>

> class bsInput {
>
> static InputStreamReader is = new InputStreamReader(System.in);
> static BufferedReader br = new BufferedReader(is);
>
> public static int readX() throws IOException
> {
> String input = br.readLine();
> int i = Integer.parseInt(input);
> return i;
> }
>
> <code snip>
>
>
>
> It compiles, but I get this error when trying to run it:
>
> Enter x-coordinate (a-j): d
> Exception in thread "main" java.lang.NumberFormatException: d
> at java.lang.Integer.parseInt(Integer.java:414)
> at java.lang.Integer.parseInt(Integer.java:463)
> at bsInput.readX(coord.java:42)
> at coord.main(coord.java:15)
>

The problem is 'd' is not an integer! If you want 'd' to

represent an int then you need to code something that
calculates or looks up a value for 'd'

'a' --> 0
'b' --> 1 etc.

for example

String lookup = "abcdefghij";

int i = -1;
while (i == -1) {
String input = br.readLine();
i = lookup.indexOf(input);
}
return i;

Haven't run this, but it should work.

Jim

0 new messages