首先让我说我是Java的新手,如果我犯了明显的错误,请原谅我......
我有一个文本文件,我必须从中读取数据并将数据拆分成单独的数组。
文本文件包含此格式的数据(尽管如果有必要,可以稍微修改一下,如果它是唯一的方法,则可以使用标识符标记)
noOfStudents
studentNAme studentID numberOfCourses
courseName courseNumber creditHours grade
courseName courseNumber creditHours grade
courseName courseNumber creditHours grade
。
。
studentNAme studentID numberOfCourses
courseName courseNumber creditHours grade
courseName courseNumber creditHours grade
courseName courseNumber creditHours grade
。
。
第一行表示将列出的“学生”总数,需要将其移动到数组。
一个数组将包含学生信息,所以
studentName,studentID,numberOfCourses
到一个数组,和
courseName,courseNumber,creditHours,grade
到第二个阵列。
我的问题源于如何解析这些数据 我正在读第一行,转换为int并使用它来确定我的学生数组的大小。 之后,我对如何将数据移动到数组并让我的程序知道哪个数组移动哪些数据线感到茫然。
有一点需要注意的是,每个学生的课程数量是可变的,所以我不能简单地将1行读入一个数组,然后将3行读入下一个数组,等等。
我是否需要使用标识符,或者我错过了一些明显的东西?我一直在看这个问题一个星期了,此时我只是感到沮丧。
非常感谢任何帮助!谢谢
编辑:这是我目前正在处理的代码部分。
public static void main(String args[])
{
try{
// Open the file
FileInputStream fstream = new FileInputStream("a1.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine; // temporarily holds the characters from the current line being read
String firstLine; // String to hold first line which is number of students total in file.
// Read firstLine, remove the , character, and convert the string to int value.
firstLine = br.readLine();
firstLine = firstLine.replaceAll(", ", "");
int regStudnt = Integer.parseInt(firstLine);
// Just to test that number is being read correctly.
System.out.println(regStudnt + " Number of students\n");
// 2D array to hold student information
String[][] students;
// Array is initialized large enough to hold every student with 3 entries per student.
// Entries will be studentName, studentID, numberOfCourses
students = new String[3][regStudnt];
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Split each line into separate array entries via .split at indicator character.
// temporary Array for this is named strArr and is rewriten over after every line read.
String[] strArr;
strArr = strLine.split(", ");
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
我希望这有助于某人引导我朝着正确的方向前进。
我想从这一点开始我遇到的主要问题是找到如何循环学生信息被读取到学生数组,然后将课程信息读到相应的课程数组位置,然后重新开始与所有学生一起读新学生。
答案 0 :(得分:1)
尝试使用此代码段,我认为它完全符合您的要求。如果您有任何困惑请告诉我!
class course {
String name;
int number;
int credit;
String grade;
}
class student {
String name;
String id;
int numberCourses;
course[] courses;
}
class ParseStore {
student[] students;
void initStudent(int len) {
for (int i = 0; i < len; i++) {
students[i] = new student();
}
}
void initCourse(int index, int len) {
for (int i = 0; i < len; i++) {
students[index].courses[i] = new course();
}
}
void parseFile() throws FileNotFoundException, IOException {
FileInputStream fstream = new FileInputStream("test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int numberStudent = Integer.parseInt(br.readLine());
students = new student[numberStudent];
initStudent(numberStudent);
for (int i = 0; i < numberStudent; i++) {
String line = br.readLine();
int numberCourse = Integer.parseInt(line.split(" ")[2]);
students[i].name = line.split(" ")[0];
students[i].id = line.split(" ")[1];
students[i].numberCourses = numberCourse;
students[i].courses = new course[numberCourse];
initCourse(i, numberCourse);
for (int j = 0; j < numberCourse; j++) {
line = br.readLine();
students[i].courses[j].name = line.split(" ")[0];
students[i].courses[j].number = Integer.parseInt(line.split(" ")[1]);
students[i].courses[j].credit = Integer.parseInt(line.split(" ")[2]);
students[i].courses[j].grade = line.split(" ")[3];
}
}
}
}
您可以在执行students
后打印ParseStore
数组的内容进行测试
答案 1 :(得分:0)
新学生开始时有一个标识符(可能是一个空行)会让你很容易,就像你可以做的那样
if("yourIdentifier".equals(yourReadLine))
<your code for starting a new student>
答案 2 :(得分:0)
这是一些可以让你按计划进行的伪代码:
Student[] readFile() {
int noOfStudents = ...;
Student[] students = new Student[noOfStudents];
for (int i = 0; i < noOfStudents; ++i) {
students[i] = readStudent();
}
return students;
}
Student readStudent() {
int numberOfCourses = ...;
String name = ...;
String id = ...;
Course[] courses = new Course[numberOfCourses]
for (int i = 0; i < numberOfCourses; ++i) {
courses[i] = readCourse();
}
return new Student(id, name, courses);
}