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:
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.
Create a
Class called StudentInside the class use
instance variablelikeString firstName;
String lastname;
Boolean isTuitionPaid; // Boolean cause isPaid will be true or false
String[] courses;
int creditHours;
String grade;
Create a constructor of this class which takes the following arguments in its parameter
Student( String fName,String lName,Boolean istPaid,String[] course,int cHours,String gr)
When you read the data of a student from a file, store it in the appropriate data type, as mentioned in the constructor, then create the Object of type StudentAfter creating the
Student object with the data, store it an appropriate Collection.ArrayList, Map, etc