aspose file tools*
The moose likes Beginning Java and the fly likes Why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why "539" and not "1"?" Watch "Why "539" and not "1"?" New topic
Author

Why "539" and not "1"?

Robert Ploch
Greenhorn

Joined: Jun 05, 2002
Posts: 19
hi
i have to to create a method which is able to read a string, check, if it is a correct number (e.g. +10, -50, 10, but not +5w8) and return that number as an int
i coded this so far:

when i enter "1" as a string --> number = 539, which shouldnt be!!
any idea someone?
thx
[ June 05, 2002: Message edited by: Dirk Schreckmann ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879


Am I missing something? what is the index variable for?
Jamie
Robert Ploch
Greenhorn

Joined: Jun 05, 2002
Posts: 19
the index should be the position in the array, for example: if index=1, and inputString=123, the number will be generated out of 2 and 3, if index=0, the whole number will be taken into account
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I thought that might be the case. try this instead

you might have to deal with the NumberFormatException in case of a conversion error.
Jamie
[ June 05, 2002: Message edited by: Jamie Robertson ]
Robert Ploch
Greenhorn

Joined: Jun 05, 2002
Posts: 19
uff, i`m a newbie and dont know exactly, what you mean with Numberformatexception etc.
anyway, if i replace "return number" with
"return Integer.parseInt( inputString.substring( index ) )", an error occurs
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, what does the error message say? And what is the value of the inputString and index you're using when it occurs?


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Also Bulla, please change your display name to include a first and last name, as discussed in our display name policy. Thanks.
Robert Ploch
Greenhorn

Joined: Jun 05, 2002
Posts: 19
ok
the following error occurs, if inputString=12345 and index=0
java test
please type in a string: 12345
java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Compiled Code)
at java.lang.Integer.parseInt(Integer.java:375)
at MakeNumberAutomat.makeNumber(Compiled Code)
at test.main(test.java:12)

the goal is: check, if a string is a real number, and calculate a number out of a string using that formula:
((((1*10+2)*10+3)*10+4)*10+5)*10..etc.
that number should be returned.
if the inputString is <0, the number should be mulitiplicated with (-1) and also be returned
the inputString might also be "+12345" or "-12345" and even " +12345"
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Thanks, Robert. Any relation to the author of "Psycho"?
The NumberFormatException indicates that Integer.parseInt() doesn't like the format of the input string - it doesn't look like a valid number. But this isn't making much sense, as it seems that input should parse without a problem. Perhaps the input isn't being passed to the Integer.parseInt() properly? I suggest showing the somplete code you currently have for this. Also, try inserting a few lines:

This way you can see the results of each step, and know exactly what's going into the Integer.parseInt() just before the error occurs. You can remove the print statements later when you're happy with how everything works.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Robert, note that you may soon discover that
Integer.parseInt( "+123" );
would throw a NumberFormatException.
You may wish to take a look at this old conversation in which I stumbled through learning about the location of the standard API source files and the basics of using jar files after coming across the above problem unexpectedly.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bulla:
when i enter "1" as a string --> number = 539, which shouldnt be!!
any idea someone?

It's because (int)'1' doesn't give you the integer value 1 but the unicode value of the character '1'.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

mabe one of these can help you accomplish your task...or give you some ideas

Jamie
Robert Ploch
Greenhorn

Joined: Jun 05, 2002
Posts: 19
thx jamie, numberformat1 was the right one, i had to modify it a littlebit cause an introducing '+' would return a wrong result, seems to be a problem in java
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Note that the massive switch is not really necessary. You can use:
char c = ...;
if (c >= '0' && c <= '9') {
int currentDigit = c - '0';
} else {
// it isn't a digit. What could it be...
}
 
 
subject: Why "539" and not "1"?