0

Hi,
I am trying to use an exception catch properly but I am getting lost and frustrated. I've changed my code so many times and cannot figure out why when it catches the exception it will not let me continue to enter data into the array. It just displays it and all entries after the bad input is null(0). any help would be greatly appreciated. Have a nice day, C.D.
below is the output and then the code;

Please enter a test score for student I.D.# 101 >> 106
You have entered an invalid score for this student they will receive a 0 for the test!!!

Please enter a test score for student I.D.# 102 >> 98
Please enter a test score for student I.D.# 103 >> 86
Please enter a test score for student I.D.# 104 >> fifty
You have not entered a digit the student will receive a zero for this test!!!
The Student's Scores are as follows:
Student I.D.# 101received a 0.0 on the test.The Student's Scores are as follows:
Student I.D.# 102received a 98.0 on the test.The Student's Scores are as follows:
Student I.D.# 103received a 86.0 on the test.The Student's Scores are as follows:
Student I.D.# 104received a 0.0 on the test.The Student's Scores are as follows:
Student I.D.# 105received a 0.0 on the test.The student(s) with I.D. number(s) of 101 , received a score of zero due to an invalid score entry of 106.0
BUILD SUCCESSFUL (total time: 20 seconds)

package testscore;

import java.util.*;

public class TestScore {
    private final int[] studentID = {101,102,103,104,105};
    private double[] studentScore = new double[5];
    private final double MAX_SCORE = 100;
    private final double MIN_SCORE = 0;
    public static int x;
    Scanner input = new Scanner(System.in);
    private double tempScore;
    private static ArrayList invalidEntries = new ArrayList();
    private static ArrayList invalidScores = new ArrayList();




    public void setScores() throws ScoreException, InputMismatchException
    {
        for( x = 0; x < studentID.length; ++x)
        {
            System.out.print("Please enter a test score for student I.D.# " +
                studentID[x] + " >> ");
                tempScore = input.nextDouble();                


                if(tempScore > MAX_SCORE || tempScore < MIN_SCORE)
                {                    
                System.out.println("You have entered an invalid score for this "
                        + "student they will receive a 0 for the test!!!\n");
                invalidEntries.add(studentID[x]);
                invalidScores.add(tempScore);
                studentScore[x] = 0;                
                }
                else
                studentScore[x] = tempScore;
         }
        input.nextLine();
    }          
    public void displayScores()
        {
            for( x = 0; x < studentScore.length; ++x)
            {
                System.out.print("The Student's Scores are as follows:\n");
                System.out.print("Student I.D.# " + studentID[x] + 
                        "received a " + studentScore[x] + " on the test." );
            }
        }

    public void displayerrors()
    {
        for(int z = 0; z < invalidEntries.size(); ++z)
            {
                System.out.print("The student(s) with I.D. number(s) of " );
                System.out.print(invalidEntries.get(z) + " , " + 
                        "received a score of zero due to an invalid score " + 
                        "entry of " +invalidScores.get(z) + "\n");
            }
    }

    public static void main(String[] args) 
    {
        TestScore currentScores = new TestScore();
        String error = new String();
        Scanner input = new Scanner(System.in);

        try
        {
        currentScores.setScores();

        }

        catch(ScoreException num)
        {   
            error = num.getMessage();

        }
        catch(InputMismatchException word)
        {
            System.out.print("You have not entered a digit the student " +
                    "will receive a zero for this test!!!");
            currentScores.tempScore = 0;
        }
        input.nextLine();


        currentScores.displayScores();
        currentScores.displayerrors(); 
    }    
}



public class ScoreException extends Exception 
{

    public ScoreException(String zero)
    {         
       zero = "Score cannot be less that zero or greater than one hundred !!!";

    }
}
2
Contributors
1
Reply
9
Views
2 Years
Discussion Span
Last Post by JamesCherrill
0

The problem is that you throw the exception from the setScores method, which doesn't allow you to recover or continue after the exception. Data format errors are best handled inside the data entry code, something like (pseudo code) for each item that needs to be entered

boolean needValidInput = true;
while (needValidInput) {
   try {
      get one data item
      needValidInput = false // we now have valid input             
   }  catch invalid input exception {
      display error
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.