How to store multiple variables from a File Input of unknown size in Java?

Posted by AlphaOmegaStrife on Stack Overflow See other posts from Stack Overflow or by AlphaOmegaStrife
Published on 2012-06-10T16:21:29Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 201

Filed under:
|
|
|
|

I'm a total beginner with my first programming assignment in Java. For our programming assignment, we will be given a .txt file of students like so:

3 345
Lisa Miller 890238 Y 2 <-(Number of classes)
Mathematics MTH345 4 A
Physics PHY357 3 B


Bill Wilton 798324 N 2
English ENG378 3 B
Philosophy PHL534 3 A


Dandy Goat 746333 Y 1
History HIS101 3 A"

The teacher will give us a .txt file on the day of turning it in with a list of unknown students.

My problem is: I have a specific class for turning the data from the file into variables to be used for a different class in printing it to the screen. However, I do not know of a good way to get the variables from the input file for the course numbers, since that number is not predetermined. The only way I can think of to iterate over that unknown amount is using a loop, but that would just overwrite my variables every time. Also, the teacher has requested that we not use any JCL classes (I don't really know what this means.)

Sorry if I have done a poor job of explaining this, but I can't think of a better way to conceptualize it. Let me know if I can clarify.

Edit:

public static void analyzeData()
{
    Scanner inputStream = null;

    try
    {
        inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
    }

    catch (FileNotFoundException e)
    {
        System.out.println("File Programming Assignment 1 Data.txt could not be found or opened.");
        System.exit(0);        
    }

    int numberOfStudents = inputStream.nextInt();
    int tuitionPerHour = inputStream.nextInt();
    String firstName = inputStream.next();
    String lastname = inputStream.next();
    String isTuitionPaid = inputStream.next();
    int numberOfCourses = inputStream.nextInt();
    String courseName = inputStream.next();
    String courseNumber = inputStream.next();
    int creditHours = inputStream.nextInt();
    String grade = inputStream.next();

To show the methods I am using now, I am just using a Scanner to read from the file and for Scanner inputStream, I am using nextInt() or next() to get variables from the file. Obviously this will not work when I do not know exactly how many classes each student will have.

© Stack Overflow or respective owner

Related posts about java

Related posts about homework