• Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalArgumentException and an Array Constructor  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So What I need to do is
have a constructor accept an array of test scores as an argument.
Class should have a get average method
If any test score in the array is less than 0 or over 100 throw an IllegalArgumentException.



My Error Message Class



My Main Class


Not finished.. but i'm not so sure I am heading in the right direction.
Any wise words would be greatly appreciated!
Thanks in advance
 
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch

There are a few things to consider. There is already an IllegalArgumentException class that extends Exception.
So why create something similar of your own?
Where is the exception actually thrown?
 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Exception needs to be thrown each time a user inputs a test score.

These really have me confused and I'm not sure where to go with them at all..

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked the link I posted above?
 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, so the IllegalArgumentException is built into java?

So I am assuming I need to do

import java.lang.*;

at the top in order to use it,

But do I still need to create this class? Or do I just call it directly into my main class code?


I am also stuck on how my try / catch statement would look as the way I have it, theres no where that I have told it that it needs to be greater than 0 and less than 100 in order for the error to appear.


Very lost on these.. it was a necessity to come to you guys with this one, I've read through my book and tried to compare mine to some examples and nothing seems to go right together with this. :/

 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I have in my main. Getting an Error on the IllegalArgumentException

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote:Yep, so the IllegalArgumentException is built into java?

Yes, you can use the same one.
Tony Burton wrote:So I am assuming I need to do

import java.lang.*;

No need, the compiler does that automatically for you.

Tony Burton wrote:But do I still need to create this class? Or do I just call it directly into my main class code?

Use it directly.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote:Here is what I have in my main. Getting an Error on the IllegalArgumentException

What error are you getting? Is the error description not helpful in understanding what is wrong?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically you are missing the part of the code where the exception is actually thrown.
I mean there should be a condition check if the score value is within the said limits, if not throw the exception.
 
Marshal
Posts: 56600
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you are throwing the Exception, it is usually bad design to catch it in the same place. Your constructor will have some sort of test to validate its input, and if the input is invalid, throw the Exception and let some other method catch it. The reason is that your constructor probably cannot do anything about the exception.
Concrete example: Let's have a constructor which takes a positive number. You state this by putting it in the documentation comments.If you look in the documentation for IllegalArgumentException (IAE), you find RuntimeException amongst its superclasses ∴ IAE is unchecked, ∴ there is no need to use the throws keyword. You can find out more about throws in the Java Tutorials. In fact, you would do well to read the whole Java Tutorials “trail”.

Now, you put a test inside the constructor to see whether that requirement is fulfilled. Much of the following code is copied unchanged from above.If number is indeed ≤ 0, as in the documentation comment, all you need to do is throw the appropriate Exception. You can pass a message to its constructor, if you wish, maybe "This number parameter must be positive in XXX constructor." Or something more poetic and grammatical

By throwing that Exception, you have signalled to the calling method that it was a mistake to pass 0, -999 or whatever. The constructor has no way to work out what the correct value ought to have been, but the calling method might. You might put the call in a loop and keep repeating the loop until a positive value is obtained.
Or, you might not do anything about the unchecked Exception and it keeps going until it gets to the main method and you get it thrown and the current thread terminates and the JVM prints a stack trace. If that happens in development, you can think, “Look, somehow we have managed to pass a non‑positive value to that constructor and we aren't supposed to.” Then you can change the code which calls that constructor and make sure only positive values are passed.

And welcome again
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote:Not finished.. but i'm not so sure I am heading in the right direction.
Any wise words would be greatly appreciated!

Bascially: follow Campbell's advice.

It seems that what you're trying to do is to use Exceptions to control flow, which is not normally a very good idea; however, keyboard input is quite tricky in this respect, since the Scanner class itself may throw Exceptions as the result of some idiot user.

If, for example, you ask for a number with nextInt() and the user inputs "XYZ", it will throw an InputMismatchException - however, if s/he inputs "987", it is a perfectly valid number, just not in the range that you want; and the two things are quite different: One is an exception mandated by the Scanner class, the other is a constraint that your program has added.

My suggestion: Wrap the entire business of entering a single valid number in a method; perhaps something like:and have that method continue to loop until the user enters a valid numeric string that represents a number within the supplied bounds.
Then your code becomes something like:and you hide all that error checking inside the method that is responsible for it.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
Bascially: follow Campbell's advice.
See, somebody has agreed with me. It does happen occasionally
. . . Wrap the entire business of entering a single valid number in a method; perhaps something like: . . . and you hide all that error checking inside the method that is responsible for it. . . .
And search this forum and the “Java in General” forum for “Scanner hasNextInt” and “utility class” and you can restrict yourself to user number 109202 (=me), but use “any date”. There is a good chance you will find enough bits of utility classes to be able to construct a method which does exactly what Winston is suggesting.

Now you will have a utility class which you can reuse whenever you need it
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Bascially: follow Campbell's advice. . . .
But watch out for spelling. I keep on finding and correcting spellling erorrs in that post.
 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Something like this? This seems to work.. although I am not sure if this is what the assignment is necessarily looking for.
Im not so sure I used the IllegalArgumentException. I just kind of made my own error message. Is there a way to implement the IllegalArgumentException into my code from what I have here?

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote:I just kind of made my own error message. Is there a way to implement the IllegalArgumentException into my code from what I have here?

IMO Having your own error message and letting the program continue is the usual practice when dealing with simple user input programs.
If the assignment requires you to throw an IllegalArgumentException and quit, then you should read about throwing exceptions from your code.
 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thank you guys for the links and help!
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that means your problem has been solved. If so you can request the topic to be resolved and give thumbs up to posts that helped you find the solution.
This helps folks with similar questions.

Happy ranching
 
Tony Burton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys gave me all the resources to solve it, I didn't get it 100% figured out, I just kinda went with my own created error method that I posted. Im gonna hope for the best with that haha.

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, here we believe in helping you learn the concept than spoon feed it. Anyone would have posted the final working code that solves the problem.
But that would have defeated the purpose of making you understand. Do-it-yourself is the best way to learn and understand anything.

To make your code 100% (that is to throw the exception when the input is wrong) refer the link in my previous post to understand how exceptions are thrown by user code.
If you still have doubts, post them here.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote:So Something like this? . . .
No.

I see, however, that you have had some helpful suggestions after that post. What have you got now? You probably only need to show us the constructor where you are passing the argument.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Burton wrote: . . . I just kinda went with my own created error method that I posted. Im gonna hope for the best with that haha.

I am afraid that will not get you very far. I think you have had enough information to throw an IAE. And, as Amit G has already said, you are supposed to throw an IAE. The exercise is obviously (at least partially) about throwing Exceptions. Not throwing an Exception will lose you lots of marks. And I believe you can get your method to throw such an exception with the information you have been given and ten minutes' work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!