• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassGrader using arrays  RSS feed

 
Ryan Elder
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking an online class and the projects went from easy to difficult real fast. Now i think I just don't understand the material enough to program this project.
The program takes a .txt file and changes it into a .csv in excel. Takes the names and grades in the file to match up with a name, midterm, project, final test, and average of the grades section above.
There are three classes , a Finalproject, Student and Grade.

Here is what my teacher said to do for the part I am stuck on:

The grades[] array in the Student class is made up of Grade objects, not int primitives.
The format of the input file is exactly the same as the ClassGrader project, i.e., there are 3 lines of grade information after the student name, so setGradesFromFile (method of the Student object just created from the line of text with the name) should be reading those 3 lines, only, and putting the result of each line into grades[].
Then you need to read 3 lines in the file, i.e., in a loop (for (int i = 0; i < 3; i++)). If you do not use a loop, you would need to repeat that code you have 3 times (plus another step).
Use the int grade value you have to create (i.e., new) a Grade object, and use the loop index to put that object into the grades array.

My attempt on the Student Class:



I have no clue what I am doing for the setGradesFromFile method.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, my understanding is that you need to read three lines from a file and put them into a Grade array?

You have a Scanner that is (presumably) initialized to the text file, so all you should need to do is call the nextLine() method on that scanner to read a line from the file. After reading the line, you can create a new Grade object (you should have some constructor that takes a String in Grade) and put it into the array. You don't really need a loop for 3 lines of code, but that can work too, I guess.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Maybe a loop is the right thing to use, even for three lines, but are you sure you have three lines? To avoid any risk of errors, use a while loop and use one of the hasNextXXX methods of the Scanner. You should not rely on a number literal because it is error‑prone.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:

The outer parentheses are not needed. You probably got this from the written instructions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!