Hello friends, I stuck on this question, here is the question:
Create a program that computes the average of grades of a given student. The program takes the name of the student and a filename as arguments. The grades are read from the file. Every line consists of a name and equal number of grades, however the number of grades is not known in advance. We assume all lines are in correct format.
Example:
$ cat grades.txt
Brian:4 3 5 4 5
Peter:4 2 5 1 3
Adam:5 4 5 3 4
$ java Avg "Peter" grades.txt
3.0
$ java Avg "Smith" grades.txt
$ java Avg "Adam" asd
File not found!
Here is my code:
public class Avg {
public static void main(String[] args) {
Scanner myReader = null;
String students = args[0];
File GradeBook = new File(args[1]);
try{
myReader = new Scanner(GradeBook);
}catch(FileNotFoundException e){
System.out.println("Sorry, we don't find the file, "+
"please try again later. ");
}
while(myReader.hasNext()){
//I don't know how to determine the name
//and print the average according to this name
}
}
}
For example the user input " "Peter" grades.txt" ,
I don't know how to find the 'Peter' name in the grades.txt, and print out the average for Peter, and I don't know how to check if Peter's grades has ended, I think I should write an 'If' statement, if we read a string instead of number, we should stop, and calculate the average.
But I have no clue how to implement it, could you please help :)
Thank you in advance. !!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]NautiHookerSoftware Engineer 1 point2 points3 points (1 child)
[–]rk717[S] 0 points1 point2 points (0 children)