まず、私はJavaにかなり慣れていないので、明らかな間違いを犯している場合はご容赦ください...
データを読み取り、データを別々の配列に分割する必要があるテキストファイルがあります。
テキストファイルには、この形式のデータが含まれています(ただし、必要に応じて、それが唯一の方法である場合は、識別子タグを持つように少し変更することができます)
noOfStudents
studentNAme
studentIDnumberOfCoursescourseNamecourseNumbercreditHoursgradecourseNamecourseNumbercreditHoursgradecourseNamecourseNumbercreditHoursgrade
。
_
_
。
studentNAme StudentIDnumberOfCoursescourseNamecourseNumbercreditHours
グレード
courseNamecourseNumbercreditHoursグレード
courseNamecourseNumbercreditHoursグレード
。
。
最初の行は、リストされ、アレイに移動する必要がある「学生」の総数を示しています。1つの配列には学生情報が含まれるため
、studentName、studentID、numberOfCoursesは
1つの配列になり、
courseName、courseNumber、creditHours、grade
は2番目の配列になります。
私の問題は、このデータを解析する方法に起因しています。
私は現在、最初の行を読んでいて、intに変換し、それを使用して生徒の配列のサイズを決定しています。その後、データを配列に移動する方法がわからなくなり、どの行をどの配列に移動するかをプログラムに認識させることができます。
注意すべき点の1つは、各学生が受講するコースの数は可変であるため、1行を1つの配列に、次に3行を次の配列に単純に読み取ることはできないということです。
識別子を使用する必要がありますか、それとも明らかな何かが欠けていますか?私はこの問題を1週間見てきましたが、この時点で私はただイライラしています。
どんな助けでも大歓迎です!ありがとうございました
編集:これが私が現在取り組んでいるコードセクションです。
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());
}
}
これが誰かが私を正しい方向に導くのに役立つことを願っています。
この時点から私が抱えている主な問題は、学生情報が学生配列に読み込まれ、次にコース情報が適切なコース配列の場所に読み込まれ、新しいものからやり直す方法を見つけることだと思います。すべての学生が読まれるまで学生。