使用java读取包含混合数据的文件
你好,我想知道我是否能得到一些帮助。我的任务是读取文件并将信息存储到数组中。我有三个数组用于提取数据,例如如下所示:使用java读取包含混合数据的文件,java,Java,你好,我想知道我是否能得到一些帮助。我的任务是读取文件并将信息存储到数组中。我有三个数组用于提取数据,例如如下所示: Smith Jr., Joe 111-22-3333 100 90 80 70 60 88 Jones, Bill 111-11-1111 90 90 87 88 100 66 Brown, Nancy 222-11-1111 100 100 100 100 100 100 到目前为止,我读取数据的代码如下: public static void readFile(String
Smith Jr., Joe
111-22-3333 100 90 80 70 60 88
Jones, Bill
111-11-1111 90 90 87 88 100 66
Brown, Nancy
222-11-1111 100 100 100 100 100 100
到目前为止,我读取数据的代码如下:
public static void readFile(String[] studentList, String[] idList, int[] scoreList) throws IOException {
String fileName;
System.out.println("Enter file name and location if necessary: ");
fileName = KB.nextLine();
File f1= new File(fileName); // "C:\Users\User\Desktop\final.txt"
Scanner fileIn = new Scanner(f1);
int i = 0 ;
if (f1.length() == 0) {
System.out.println("File is empty");
}
while(fileIn.hasNext() && i < studentList.length){
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.nextLine();
scoreList[i] = fileIn.nextInt();
i++;
}
我已经尝试过故障排除,似乎发生的是,我的第二个数组存储了整行,而我的整数没有被读取并存储到第三个数组中
我试图将我的第二个数组设置为长数组,以拾取像111-111-111这样的数字,并将其与整数区分开来,但这给了我另一个错误,似乎是我的长数组根本没有从第二行拾取任何内容
有什么建议吗
非常感谢您抽出时间。请查看以下代码:
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.nextLine();
scoreList[i] = fileIn.nextInt();
你正在逐行阅读
但是111-22-33331009080706088
是一行(读入idList[i]
)
您需要以某种方式更改代码,将111-22-3333 100 90 80 70 60 88
读作一行,然后将该行拆分为两个(或更多,由您决定)段,进一步处理以获得ID和分数。查看此代码:
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.nextLine();
scoreList[i] = fileIn.nextInt();
你正在逐行阅读
但是111-22-33331009080706088
是一行(读入idList[i]
)
您需要以某种方式更改代码,即您将
111-22-3333 100 90 80 70 60 88
作为一行阅读,然后将该行拆分为两段(或更多段,由您决定),进一步处理以获得ID和分数。作为Moritz-Petersens答案的替代方法,您可以使用next()
method而不是nextLine()
method通过如下更改while循环来获取idList()
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.next();
scoreList[i] = fileIn.nextLine();
i++;
这将把第一个令牌(您的ID)存储到idList数组中,并将该行的剩余部分存储到scoreList数组中。我想你会在这里发生的
参考:
希望这有帮助
更新
我刚刚注意到您的记分表数组是int[]类型 基于此,上述修复将不起作用 您需要创建一个临时字符串,该字符串接受所有分数,然后将其拆分以将适当的整数放入分数数组中。 另外,创建一个新的整数用于跟踪记分表数组的索引 有点像
int scoreIndex = 0;
while(fileIn.hasNext() && i < studentList.length){
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.next();
String scores = fileIn.nextLine();
String[] splitScores = scores.trim.split(" ");
for (String a : splitScores) {
scoreList[scoreIndex] = Integer.parseInt(a);
scoreIndex++;
}
i++;
}
int scoreIndex=0;
while(fileIn.hasNext()&&i
此外,由于您使用的是原始数组数据类型作为记分表数组,因此请确保数组的大小为科目数×学生数
在您的示例中,6*3.作为Moritz-Petersens答案的替代,为了使代码的更改最小化,您可以使用
next()
方法而不是nextLine()
方法,通过如下更改while循环来获取idList()
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.next();
scoreList[i] = fileIn.nextLine();
i++;
这将把第一个令牌(您的ID)存储到idList数组中,并将该行的剩余部分存储到scoreList数组中。我想你会在这里发生的
参考:
希望这有帮助
更新
我刚刚注意到您的记分表数组是int[]类型 基于此,上述修复将不起作用 您需要创建一个临时字符串,该字符串接受所有分数,然后将其拆分以将适当的整数放入分数数组中。 另外,创建一个新的整数用于跟踪记分表数组的索引 有点像
int scoreIndex = 0;
while(fileIn.hasNext() && i < studentList.length){
studentList[i] = fileIn.nextLine();
idList[i] = fileIn.next();
String scores = fileIn.nextLine();
String[] splitScores = scores.trim.split(" ");
for (String a : splitScores) {
scoreList[scoreIndex] = Integer.parseInt(a);
scoreIndex++;
}
i++;
}
int scoreIndex=0;
while(fileIn.hasNext()&&i
此外,由于您使用的是原始数组数据类型作为记分表数组,因此请确保数组的大小为科目数×学生数
在您的示例中,6*3。您需要对代码进行以下更改
studentList[i] = fileIn.nextLine();
String id_score = fileIn.nextLine();
String[] split = id_score.split(" ",2);
idList[i] = split[0];
scoreList[i] = split[1];
.split()
将在第二行分隔id和分数。您需要对代码进行以下更改
studentList[i] = fileIn.nextLine();
String id_score = fileIn.nextLine();
String[] split = id_score.split(" ",2);
idList[i] = split[0];
scoreList[i] = split[1];
.split()
将在第二行中分隔id和分数。您试图在哪个变量中读取什么数据?我试图将名称读入字符串数组,将xxx xxx xx id读入另一个字符串数组,然后将数字读入整数数组。为什么要在单独的列表中跟踪学生的属性?您可以创建一个包含所有数据的类Student
。这就是你使用Java的方式,顺便说一句……我想我应该澄清一下,这是一个作业,我的教授要求将属性存储在数组中。你试图在哪个变量中读取什么数据?我试图将名称读入一个字符串数组,将xxx xxx xx id读入另一个字符串数组,接下来的数字是一个整数数组。为什么要在单独的列表中记录学生的属性?您可以创建一个包含所有数据的类Student
。顺便说一句,这就是你使用Java的方式……我想我应该澄清一下,这是一个作业,我的教授要求将属性存储在数组中。是的,我已经输出了数组以进行故障排除,我注意到了这一点,我只是在努力弄清楚如何将数字行分割成段。谢谢您的回复。@kcode是的,我已经输出了阵列以进行故障排除,我注意到了这一点,我只是在挣扎