我有一個文本文件我正在嘗試與字符串標記器分離。這裏是幾行文本文件:字符串標記停止在第一行後
Mary Smith 1
James Johnson 2
Patricia Williams 3
我想分解成名字,姓氏和客戶ID。
到目前爲止,我已經能夠做到這一點,但它停止後,瑪麗史密斯。
這裏是我的代碼:
public static void createCustomerList(BufferedReader infileCust,
CustomerList customerList) 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);
firstName = st.nextToken();
while(st.hasMoreElements())
{
lastName = st.nextToken();
custId = Integer.parseInt(st.nextToken());
CustomerElement CustomerObject = new CustomerElement();
CustomerObject.setCustInfo(firstName,lastName,custId);
customerList.addToList(CustomerObject);
}
}
我應該爲每個變量聲明使用camelCase。在你的代碼中'StringToBreak'和'CustomerObject'具有大寫字母,並且保留給類型(類和接口)。它有效,但會導致混淆。 – helios 2012-02-13 16:59:09