我想我把它分解成最基本的形式。如果没有,那么我很抱歉,并会尝试对其进行编辑。如果我已经将变量设置为字符串,为什么在我的 while 循环中需要为 FirstName 转换字符串?难道我做错了什么?我尝试将 FirstName 变量设置为等于第一个标记,该标记应该是文本文件的第一个单词等。
try
{
BufferedReader infileCust =
new BufferedReader(new FileReader("C:\\custDat.txt"));
}
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.toString());
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
public static void createCustomerList(BufferedReader infileCust,
CustomerList custList) throws IOException
{
String FirstName;
String LastName;
int CustId;
//take first line of strings before breaking them up to first last and cust ID
String StringToBreak = infileCust.readLine();
//split up the string with string tokenizer
StringTokenizer st = new StringTokenizer(StringToBreak);
while(st.hasMoreElements()){
FirstName = (String) st.nextElement();
LastName = (String) st.nextElement();
CustId = Integer.parseInt((String) st.nextElement());
}
编辑:显然我可以使用 nextToken() 代替。但是为什么我的变量显示为未使用?它们不在while循环的范围内吗?