Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing data from text file into multiple arrays in Java
    primarykey
    data
    text
    <p>Let me start by saying I am fairly new to Java so forgive me if I am making obvious mistakes...</p> <p>I have a text file that I must read data from and split the data into separate arrays.</p> <p>The text file contains data in this format (although if necessary it can be slightly modified to have identifier tags if it is the only way)</p> <p>noOfStudents<br> studentNAme studentID numberOfCourses<br> courseName courseNumber creditHours grade<br> courseName courseNumber creditHours grade<br> courseName courseNumber creditHours grade<br> .<br> .<br> studentNAme studentID numberOfCourses<br> courseName courseNumber creditHours grade<br> courseName courseNumber creditHours grade<br> courseName courseNumber creditHours grade<br> .<br> . </p> <p>The first line indicates the total number of "students" that will be listed and will need to be moved to arrays. One array will contain student information so<br> studentName, studentID, numberOfCourses<br> to one array, and<br> courseName, courseNumber, creditHours, grade<br> to the second array.</p> <p>My problem is stemming from how to parse this data.<br> I'm currently reading in the first line, converting to int and using that to determine the size of my student array. After that I am at a loss for how to move the data into arrays and have my program know which array to move which lines into. </p> <p>One thing to note is that the number of courses each student takes is variable so I can't simply read 1 line into one array, then 3 lines into the next, etc. </p> <p>Will I need to use identifiers or am I missing something obvious? I've been looking at this problem for a week now and at this point I'm just getting frustrated.</p> <p>Any help is greatly appreciated! thank you</p> <p>edit: Here is the code section I am working on at the moment. </p> <pre><code>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()); } } </code></pre> <p>I hope this helps someone lead me in the right direction.</p> <p>I guess the major problem I'm having from this point is finding out how to loop in such a way that the student info is read to the student array, then the course info to the appropriate course array location, then start again with a new student until all students have been read.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload