我正在尝试使用将仅包含数字的字符串转换为整数int number = Integer.parseInt(string)
,但它返回 #
error: Invalid int: "number"
例如,如果字符串为“10”,则返回:Invalid int: "10"
。怎么了?
编辑:
FileInputStream fis;
int number =0;
String line="";
try
{
fis = new FileInputStream(getFilesDir()+pathToFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
line=reader.readLine();
Log.e("Read Line", "Read line: *" +line+ "*"); //returns *10*
try
{
number = Integer.parseInt(line.trim());
}
catch(NumberFormatException nfe)
{
Log.e("ERROR CONVERTING", nfe.getMessage()); // return above error
}
reader.close();
}
catch(Exception ex)
{
Log.e("ERROR OPENING FILE", "Can't open file: "+ex.getMessage());
}