I am doing a simple exercise from a book, and I'm a little confused about how parseInt java function works. I read a line from the input file, used a StringTokenizer to split it, and now I want to parse each part as an integer.
I checked in the viewer that the input of the parseInt function is really a string that seems like a real integer (for example, "35"). However, when I try to use a function str.charAtfor my variable strwhile holding the value "35", I get strange results:
str.charAt(0) == ""
str.charAt(1) == "3"
str.charAt(2) == ""
str.charAt(3) == "5"
This is probably an encoding issue, so I tried to fix it using this method of reading a file:
InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");
(I explicitly saved the file using UTF-8 encoding in my editor), but this did not help. Any ideas what could be the problem and how to fix it?
EDIT: My sample
InputStreamReader reader = new InputStreamReader(new FileInputStream(inputfile), "UTF-8");
BufferedReader bfreader = new BufferedReader(reader);
line = bfreader.readLine();
while (line !=null)
{
String[] valueStrings = line.split(" ");
String hole = valueStrings[0];
int[] values = new int[4];
for (int i = 0; i <values.length; i++){
String nr = valueStrings[i+1].trim();
values [i] = Integer.parseInt(nr);
}
}
source
share