我需要将nextToken()的输出转换为int和double类型。我当前的方式不起作用,并给我一个错误,说java.lang.String不能转换为int。怎么办呢?谢谢!
int id,age;
String name;
double gpa;
String line = null;
while( inputStream.hasNextLine())
{
line = inputStream.nextLine();
size ++;
StringTokenizer tokens = new StringTokenizer(line," .,?\"");
while(tokens.hasMoreTokens())
{
id = (int) tokens.nextToken(); //need to convert this
name = tokens.nextToken();
age =(int) tokens.nextToken(); //need to convert this
gpa = (double) tokens.nextToken(); //need to convert this
}
}
答案 0 :(得分:1)
试试这个:
id = Integer.parseInt(tokens.nextToken());