Integer output in Java method not same as pre-converted char value
I'm trying to parse a simple text file in an integer method and then output an inte开发者_如何学JAVAger 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).
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
精彩评论