• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why "539" and not "1"?

 
Robert Ploch
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Am I missing something? what is the index variable for?
Jamie
 
Robert Ploch
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what does the error message say? And what is the value of the inputString and index you're using when it occurs?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mabe one of these can help you accomplish your task...or give you some ideas

Jamie
 
Robert Ploch
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic