java 使用字符串标记器从文本文件中设置创建数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2173855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using string tokenizer to set create arrays out of a text file?
提问by ashamadelion
Hey. You may have recently seen a post by me looking for help, but I did it wrong before, so I am going to start fresh and begin at the basics.
嘿。您可能最近看到了我寻求帮助的帖子,但我之前做错了,所以我将重新开始并从基础开始。
I am trying to read a text file that looks like this:
我正在尝试读取如下所示的文本文件:
FTFFFTTFFTFT
3054 FTFFFTTFFTFT
4674 FTFTFFTTTFTF
... etc
FTFFTTFFTFT
3054 FTFFTTFFTFT
4674 FTFTFFTTTFTF
... 等
What I need to do is put the first line into a String as the answer key.
我需要做的是将第一行放入 String 作为答案键。
Next, I need to create an array with the student ID (the first numbers). Then, I need to create an array that is parallel to the student ID that contains the student's answers.
接下来,我需要使用学生 ID(第一个数字)创建一个数组。然后,我需要创建一个与包含学生答案的学生 ID 平行的数组。
Below is my code, and I can't quite figure out how to get it to work like this, and I was wondering if someone could help me out with it.
下面是我的代码,我不太清楚如何让它像这样工作,我想知道是否有人可以帮我解决这个问题。
public static String[] getData() throws IOException {
int[] studentID = new int[50];
String[] studentAnswers = new String[50];
int total = 0;
String line = reader.readLine();
strTkn = new StringTokenizer(line);
String answerKey = strTkn.nextToken();
while(line != null) {
studentID[total] = Integer.parseInt(strTkn.nextToken());
studentAnswers[total] = strTkn.nextToken();
total++;
}
return studentAnswers;
}
So at the end of the day, the array structure should look like:
所以在一天结束时,数组结构应该如下所示:
studentID[0] = 3054
studentID[1] = 4674
... etc
studentID[0] = 3054
studentID[1] = 4674
...等
studentAnswers[0] = FTFFFTTFFTFT
studentAnswers[1] = FTFTFFTTTFTF
studentAnswers[0] = FTFFTTFFTFT
studentAnswers[1] = FTFTFFTTTFTF
Thanks :)
谢谢 :)
采纳答案by Alex Ntousias
Assuming you have opened the file correctly for reading (because I can't see how the reader variable is initialized or the type of the reader) and the contents of the file are well-formed (according to what you expect), you have to do the following:
假设您已正确打开文件进行读取(因为我看不到 reader 变量是如何初始化的或读取器的类型)并且文件的内容格式正确(根据您的期望),您必须请执行下列操作:
String line = reader.readLine();
String answerKey = line;
StringTokenizer tokens;
while((line = reader.readLine()) != null) {
tokens = new StringTokenizer(line);
studentID[total] = Integer.parseInt(tokens.nextToken());
studentAnswers[total] = tokens.nextToken();
total++;
}
Of course it would be best if you add some checks in order to avoid runtime errors (in case the contents of the file are not correct), e.g. try-catch clause around Integer.parseInt() (might throw NumberFormatException).
当然,最好添加一些检查以避免运行时错误(以防文件内容不正确),例如围绕 Integer.parseInt() 的 try-catch 子句(可能会抛出 NumberFormatException)。
EDIT: I just notice in your title that you want to use StringTokenizer, so I edited my code (replaced the split method with the StringTokenizer).
编辑:我只是在你的标题中注意到你想使用 StringTokenizer,所以我编辑了我的代码(用 StringTokenizer 替换了 split 方法)。
回答by McDowell
You may want to think about...
你可能想考虑...
- using the
Scannerclass for tokenizing the input - using collection types (such as
ArrayList) instead of raw arrays - arrays have their uses, but they aren't very flexible; anArrayListhas a dynamic length - creating a class to encapsulate the student id and their answers - this keeps the information together and avoids the need to keep two arrays in sync
- 使用
Scanner类来标记输入 - 使用集合类型(例如
ArrayList)而不是原始数组 - 数组有其用途,但它们不是很灵活;一个ArrayList具有动态长度 - 创建一个类来封装学生 ID 和他们的答案 - 这将信息保持在一起并避免需要保持两个数组同步
Scanner input = new Scanner(new File("scan.txt"), "UTF-8");
List<AnswerRecord> test = new ArrayList<AnswerRecord>();
String answerKey = input.next();
while (input.hasNext()) {
int id = input.nextInt();
String answers = input.next();
test.add(new AnswerRecord(id, answers));
}

