【发布时间】:2012-02-13 16:47:24
【问题描述】:
我有一个文本文件,我试图用字符串标记器分解。这是文本文件的几行:
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具有首字母大写字母,这是为类型(类和接口)保留的。它有效,但会导致混乱。
标签: java stringtokenizer