Java 字符串标记器在第一行之后停止
我有一个文本文件,我正试图用字符串标记器打破它。以下是文本文件的几行:Java 字符串标记器在第一行之后停止,java,stringtokenizer,Java,Stringtokenizer,我有一个文本文件,我正试图用字符串标记器打破它。以下是文本文件的几行: Mary Smith 1 James Johnson 2 Patricia Williams 3 我试图把名字、姓氏和客户ID分开 到目前为止,我已经能够做到这一点,但在玛丽·史密斯之后就停止了 这是我的密码: public static void createCustomerList(BufferedReader infileCust, CustomerList customerList
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);
}
}
首先,你想看看你的循环,特别是你如何在循环之外拥有firstName,这样你就可以扔掉所有的标记。您将试图在没有足够信息的情况下创建新的客户对象。首先,您要查看您的循环,特别是您如何在循环之外拥有firstName,以便抛开所有标记。您将尝试在没有足够信息的情况下创建新的客户对象
String StringToBreak = infileCust.readLine();
从文件中读取第一行。然后将其输入StringTokenizer。StringTokenized找不到更多标记是正常的
您必须创建第二个循环,包含所有这些内容才能读取每一行。它是:
outer loop: readLine until it gets null {
create a StringTokenizer that consumes *current* line
inner loop: nextToken until !hasMoreElements()
}
实际上,你不需要做内部循环,因为你有三个不同的字段。这就足够了:
name = st.nextToken();
lastName = st.nextToken();
id = st.nextToken;
从文件中读取第一行。然后将其输入StringTokenizer。StringTokenized找不到更多标记是正常的
您必须创建第二个循环,包含所有这些内容才能读取每一行。它是:
outer loop: readLine until it gets null {
create a StringTokenizer that consumes *current* line
inner loop: nextToken until !hasMoreElements()
}
实际上,你不需要做内部循环,因为你有三个不同的字段。这就足够了:
name = st.nextToken();
lastName = st.nextToken();
id = st.nextToken;
对于外部循环,需要将当前行的内容存储在stringToBreak变量中,以便可以在循环内部访问它。 每一行都需要一个新的StringTokenizer,因此它需要位于循环内部
String stringToBreak = null;
while ((stringToBreak = infileCust.readLine()) != null) {
//split up the string with string tokenizer
StringTokenizer st = new StringTokenizer(stringToBreak);
firstName = st.nextToken();
lastName = st.nextToken();
custId = Integer.parseInt(st.nextToken());
}
对于外部循环,需要将当前行的内容存储在stringToBreak变量中,以便可以在循环内部访问它。 每一行都需要一个新的StringTokenizer,因此它需要位于循环内部
String stringToBreak = null;
while ((stringToBreak = infileCust.readLine()) != null) {
//split up the string with string tokenizer
StringTokenizer st = new StringTokenizer(stringToBreak);
firstName = st.nextToken();
lastName = st.nextToken();
custId = Integer.parseInt(st.nextToken());
}
我应该为每个变量声明使用camelCase。在代码中,StringToBreak和CustomerObject的首字母大写,这是为类型类和接口保留的。它可以工作,但会导致混乱。我应该对每个变量声明使用camelCase。在代码中,StringToBreak和CustomerObject的首字母大写,这是为类型类和接口保留的。它可以工作,但会导致混乱。好吧,我完全明白你所说的,但是我在编写外部循环时遇到了困难。我的outter循环基本上就是这样:而infileCust.readLine!=无效的{你说消耗当前行是什么意思?最后你说我不需要内部循环,所以我猜我只需要告诉标记器消耗行,但我不知道方法,似乎找不到一个..好吧,我完全明白你的意思,但是我写外部循环有困难。我的外部循环几乎是JUt this:while infileCust.readLine!=null{你说消耗当前行是什么意思?最后你说我不需要内部循环,所以我猜我只需要告诉标记器消耗该行,但我不知道方法,似乎找不到..我必须添加StringTokenizer st=new StringTokenizerstringToBreak;就在while循环之前不?我得到一个error:thread main java.lang.NullPointerException中的异常感谢您的编辑。这看起来不错,但现在它只是打印一个空行,而不是1。我必须在while循环之前添加StringTokenizer st=new StringTokenizerstringToBreak;否?我收到一个错误:thread main java.lang.NullPointerException中的异常这看起来不错,但现在它只是打印一个空行,而不是1