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

2
  • You're probably casting it whereas you should be calling pasrseInt as suggested. As a general rule there is not much reason to ever use explicit casts in Java 1.5+ Commented May 25, 2010 at 3:47
  • 1
    Post your code if you want decent advice. Commented May 25, 2010 at 4:02

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.