Computer Guy
asked on
Help With Illegal Argument Exception
Hi,
I am trying to make this code display an IllegalArgumentException if the scores entered in the array are <-1 or >100
I am working with the Try/Catch block but how can I make it do what I want?
I am trying to make this code display an IllegalArgumentException if the scores entered in the array are <-1 or >100
I am working with the Try/Catch block but how can I make it do what I want?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TestScores
{
public static void main(String[] args)
{
boolean hasError = false;
int numScores; // To hold the number of scores
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the number of test scores.
System.out.print("How many test scores do you have? ");
numScores = keyboard.nextInt();
// Create an array to hold the test scores.
double[] scores = new double[numScores];
// Get the test scores and store them
// in the scores array.
for (int index = 0; index < numScores; index++)
{
try
{
System.out.print("Enter score #" +
(index + 1) + ": ");
scores[index] = keyboard.nextDouble();
}catch (IllegalArgumentException ex)
{
hasError = true;
JOptionPane.showMessageDialog(null, "Invalid Score");
}
}
// Create a Grader object, passing the
// scores array as an argument to the
// constructor.
Scores myGrader = new Scores(scores);
// Display the adjusted average.
System.out.println("Your average is " +
myGrader.getAverage());
// Display the lowest score.
System.out.println("Your lowest test score was " +
myGrader.getLowestScore());
}
I would have thought it better to catch an out-of-range confinement, rather than try to raise an Exception on an otherwise correct type.
ASKER
Can you please provide a sample?
Well, given >>scores[index] = keyboard.nextDouble();<<, then why not just something like >if(scores[index] <-1||scores[index] >100){doSmthg();}
ASKER
Also with my posted code, if I enter in 50, 60 ,70 the average score is is suppose to be 60, but the program says 65.
Can't see the rest of your code, or how the average is arrived at.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SO like this?
for (int index = 0; index < numScores; index++)
{
System.out.print("Enter score #" +
(index + 1) + ": ");
if(scores[index] <-1||scores[index] >100)
{
JOptionPane.showMessageDialog(null, "Invalid Score");
}else{
scores[index] = keyboard.nextDouble();
}
Well, yeah, I guess it looks ok - haven't tried it. Have you?
But it looks like you post the input value to the array in any case, even if it's wrong . . . is that what you want?
But it looks like you post the input value to the array in any case, even if it's wrong . . . is that what you want?
ASKER
I have tried it and it did not work.
I want to throw an error if the grade is <-1 or >100
I want to throw an error if the grade is <-1 or >100
ASKER
Based in the users input of the grade they enter.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TestScores
{
public static void main(String[] args)
{
boolean hasError = false;
int numScores; // To hold the number of scores
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the number of test scores.
System.out.print("How many test scores do you have? ");
numScores = keyboard.nextInt();
// Create an array to hold the test scores.
double[] scores = new double[numScores];
// Get the test scores and store them
// in the scores array.
for (int index = 0; index < numScores; index++)
{
try
{
System.out.print("Enter score #" +
(index + 1) + ": ");
scores[index] = keyboard.nextDouble();
if(scores[index]<-1||scores[index]>100){ throw new IllegalArgumentException();}
}catch (IllegalArgumentException ex)
{
hasError = true;
JOptionPane.showMessageDialog(null, "Invalid Score");
}
}
// Create a Grader object, passing the
// scores array as an argument to the
// constructor.
//Scores myGrader = new Scores(scores);
// Display the adjusted average.
//System.out.println("Your average is " +
//myGrader.getAverage());
// Display the lowest score.
//System.out.println("Your lowest test score was " +
//myGrader.getLowestScore());
}}
ASKER
Thanks!
With that code, if I enter -1 as the first option there is no error.
With that code, if I enter -1 as the first option there is no error.
or . . . based on what I first said, and not using Exceptions :
-----------
btw this earlier comment of mine was horse :
"But it looks like you post the input value to the array in any case, even if it's wrong . . . is that what you want?"
I didn't read the code flow properly after the exception clause.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TestScores
{
public static void main(String[] args)
{
boolean hasError = false;
int numScores; // To hold the number of scores
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the number of test scores.
System.out.print("How many test scores do you have? ");
numScores = keyboard.nextInt();
// Create an array to hold the test scores.
double[] scores = new double[numScores];
// Get the test scores and store them
// in the scores array.
for (int index = 0; index < numScores; index++)
{
//try
//{
System.out.print("Enter score #" +
(index + 1) + ": ");
scores[index] = keyboard.nextDouble();
if(scores[index]<-1||scores[index]>100){ //throw new IllegalArgumentException();}
//}catch (IllegalArgumentException ex)
//{
hasError = true;
JOptionPane.showMessageDialog(null, "Invalid Score");
//}
}}
// Create a Grader object, passing the
// scores array as an argument to the
// constructor.
//Scores myGrader = new Scores(scores);
// Display the adjusted average.
//System.out.println("Your average is " +
//myGrader.getAverage());
// Display the lowest score.
//System.out.println("Your lowest test score was " +
//myGrader.getLowestScore());
}}
-----------
btw this earlier comment of mine was horse :
"But it looks like you post the input value to the array in any case, even if it's wrong . . . is that what you want?"
I didn't read the code flow properly after the exception clause.