Académique Documents
Professionnel Documents
Culture Documents
numeric test score for the student. Create an "Exception Handling" class, and throw
an Exception for that class if the user does not enter a valid score (greater than 0 but
less than or equal to100). Catch the exception and then display an appropriate
message. Repeat until a valid score is entered and display the studentID and score.
I think i have a handle on it for the most part. The issue is....well the first issue is I am not totally sure I am doing it write.
the second is everything else is working....except the try and catch that i am using.
here is my code.
03
04 public testScore() {
05 }
06
08 testScore = p_testScore;
09 }
10
12 return(testScore);
13 }
14
15 }
03
05 testScore = p_testScore;
06 }
07
11 else
12 return("");
13 }
14
15 }
view source
01 import javax.swing.*;
02
04
06
09
10 try {
12
13 System.exit(0);
14 }
15
16 catch (NegativeNumberException e) {
17 JOptionPane.showInputDialog(null, e.getMessage());
18 System.exit(-1);
19
20 }
21
22 }
23
24 }
3 super(msg)
4 }
5 }
In the try block you want to have the code that will generate the error and the catch block is supposed to catch the error
and run the code in the catch block. Right now, your code isn't even catching your custom created exception. Another
problem is that youre using JOptionPane which (i think) is part of a GUI yet you have no GUI defined. You should try
something more along the lines of:
view source
1 try {
2 testScore(-10);
3 }
4 catch (WrongTestScore e) {
5 System.out.println("Wrong test score entered, please try again.");
6 }
If this doesnt work try having your testScore() method throw the WrongTestScore error if an invalid number is entered.
Hope this helps.
03
04 public testScore() {
05 }
06
08 try{
09 int i = Integer.parseInt(input);
10 if (i<0 || i>100)
12 }catch(Exception e){
13 throw new Exception("wrong input blah blah");
14 }
15 }
16
18 return(testScore);
19 }
20
21 }
02
04
07 while(true){
08 try {
Jeremy.setScore(JOptionPane.showInputDialog(null,
09
"Student ID#: "+ studentID + "\nPlease enter your test Scores below."));
JOptionPane.showMessageDialog("Student ID# is: " +
10
studentID + "\nScore is: " + Jeremy.getScore());
11 break;
12 }
13 catch (Exception e) {
14 JOptionPane.showInputDialog(null, e.getMessage());
15 }
16 }
17 }
18
19 }
print?
03
04 public testScore() {
05 }
06
08 try{
09 int i = Integer.parseInt(input);
10 if (i<0 || i>100)
12 }catch(Exception e){
14 }
15 }
16
18 return(testScore);
19 }
20
21 }
print?
02
04
07 while(true){
08 try {
Jeremy.setScore(JOptionPane.showInputDialog(null,
09
"Student ID#: "+ studentID + "\nPlease enter your test Scores below."));
JOptionPane.showMessageDialog("Student ID# is: " +
10
studentID + "\nScore is: " + Jeremy.getScore());
11 break;
12 }
13 catch (Exception e) {
14 JOptionPane.showInputDialog(null, e.getMessage());
15 }
16 }
17 }
18
19 }