tags:

views:

34

answers:

1

I'm trying to parse a simple text file in an integer method and then output an integer from such file so that other parts of the program can use it. For testing purposes it also displays the character value (9 in this case). The integer value for some reason is 57. I've also tried it with another part of the text file (which in that case should be 5, but is instead 53).

After looking at an ASCII chart, I see that 57 is the ASCII version of the "symbol" 9 and that 53 is the ASCII version of the "symbol" 5. Is there any simple way I can fix this? I'm getting kind of frustrated as I'm a Java newbie (I've mostly only used FreePascal before this).

+3  A: 

It's nothing you can "fix". That's how ASCII works. If you post your code, we can show a better approach. For instance, you probably don't need to explicitly use chars at all.

If you use a BufferedReader like:

BufferedReader buffReader = ...;
...
String line = buffReader.readLine(); // line is now "9"
int parsed = Integer.parseInt(line);

parsed will be the int 9 as expected. Or you may be able to use Scanner and its nextInt

Matthew Flaschen
Thank you for your comments/answers, but I solved it by using Character.getNumericValue.
David