1

I am trying to allow the user to submit their test scores then get the total scores and the average score. I have a separate class called student to help simplify some tasks.

This is the Student Class:

public class Student {
   private String name;
   private int numOfQuizzes;
   private double totalScore;

   public Student(String name){
       this.name = name;
   }
   public String getName() {
       return name;

   }public void addQuiz(int score){
        numOfQuizzes++;
        totalScore += score;

   }public double getTotalScore() {
       return totalScore;
   }

   public double getAverageScore(){
       return totalScore/(double)numOfQuizzes;
   }
}

Then this is my main class so far.

ArrayList<String> scores = new ArrayList<String>();
    Scanner nameInput = new Scanner(System.in);
    System.out.print("What is your name? ");
    String name = nameInput.next();

    Scanner scoreInput = new Scanner(System.in);

    while (true) {
        System.out.print("Please enter your scores (q to quit): ");

        String q = scoreInput.nextLine();

        scores.add(q);

          if (q.equals("q")) {
              scores.remove("q");

       Student student = new Student(name);

       System.out.println("Students Name: " + student.getName());
       System.out.println("Total Quiz Scores: " + student.getTotalScore());
       System.out.println("Average Quiz Score: " + student.getAverageScore());
       break;
    }
  }
 }
}

This is the current output.

What is your name? tom
Please enter your scores (q to quit): 13
Please enter your scores (q to quit): 12
Please enter your scores (q to quit): 5
Please enter your scores (q to quit): q
Students Name: tom
Total Quiz Scores: 0.0
Average Quiz Score: NaN
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
b w
  • 37
  • 2
  • 8
  • 2
    You never call `addQuiz` on your `Student`. – rgettman May 07 '18 at 23:12
  • @rgettman how would I call getQuizz using the array? It doesn't seem to work with it? – b w May 07 '18 at 23:17
  • With each element of the array, parse it into an integer, then call `addQuiz`. – rgettman May 07 '18 at 23:17
  • The bigger issue is the fact that you are reading the numbers as a string rather than an int, even if you did call "addQuiz", you'd get the same result. – BlackHatSamurai May 07 '18 at 23:20
  • @rgettman i did that bu get the error: Exception in thread "main" java.lang.NumberFormatException: For input string: "q" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at finalReview.FinalReview.main(FinalReview.java:76) – b w May 07 '18 at 23:29

1 Answers1

0

When you read in your values, you need to check whether it's a string or an int, you only want to add integers. You might do something like:

try{
 do{
    String q = scoreInput.nextLine();
    if(q.equals("q"){
       //Do something, like break
       break; 
    }

 int numVal = Integer.valueOf(q); 

 scores.addQuiz(numVal); 
} catch (Exception e){
 //Handle error of converting string to int
}
}while(true); 
//Once you have all the scores, be sure to call your averageScore method
averageScore();

Once you have the scores, your average score method should be something like:

public double averageScore(){
   if(scores != null){
     for(int score : scores){
        totalScore += score; 
     }
     return totalScore/scores.size(); 
}

Your Student class might look like this:

  public class Student {
   private String name;
   private int numOfQuizzes;
   private double totalScore;
   private ArrayList<Integer> scores;

   public Student(String name){
       this.name = name;
       scores = new ArrayList<Integer>();
   }

   public String getName() {
       return name;

   }public void addQuiz(int score){
        scores.add(score); 
   }

   public double getTotalScore() {
       for(int score : scores){
           totalScore += score; 
       }
       return totalScore;
   }

public double averageScore(){
   if(scores != null){
     for(int score : scores){
        totalScore += score; 
     }
     return totalScore/scores.size(); 
}
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • having an issue with your method. Specifically scores this isn't a variable I have in my Student Class unless I need to add this to the main method? – b w May 07 '18 at 23:36
  • I think adding scores to your student class makes sense, but that is up to you. I would probably do that since a student has a score. Alternatively, you could argue that there should be a quiz class, which contains the score. It's up to you how you handle these types of things. – BlackHatSamurai May 07 '18 at 23:38
  • how would you implement scores is what I am asking? – b w May 07 '18 at 23:42
  • would you add the }try~ after the Please enter your scores part? – b w May 07 '18 at 23:56
  • As you said in an earlier comment, you're getting an error. You need to handle that. That is what the try statement is doing. – BlackHatSamurai May 08 '18 at 00:02