我需要将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
}
}
试试这个:
id = Integer.parseInt(tokens.nextToken());