• Post Reply Bookmark Topic Watch Topic
  • New Topic

try/catch and arraylist doesn't seem to be working for me  RSS feed

 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so, my arraylist isn't printing, and I think its because of the try/catch exception "catching" an error. But my getMessage() isnt saying that there is a problem...

The homework is this
"Create a UsedCarException class that extends Exception; its constructor receives a value for a vehicle identification number (VIN) that is passed to the parent constructor so it can be used in a getMessage() call. Save the class as UsedCarException.java. Create a UsedCar class with fields for VIN, make, year, mileage, and price. The UsedCar constructor throws a UsedCarException when the VIN is not four digits; when the make is not Ford, Honda, Toyota, Chrysler, or Other; when the year is not between 1990 and 2014 inclusive; or either the mileage or price is negative. Save the class as UsedCar.java. Write an application that establishes an array of at least seven UsedCar objects and handles any Exceptions. Display a list of only the UsedCar objects that were constructed successfully.  Save the file as ThrowUsedCarExceptions.java."

I think that the in UsedCar is part of my problem, but it's in my instruction to use the vinNum in the UsedCarException constructor. I'm lost. Any nudge would be appreciated. Thanks!

UsedCarException Class


UsedCar Class (left out getters and setters to keep this from getting out of control)


ThrowUsedCarException (Main) Class
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can your project compile ? Did you saw Enter VIN: in the console ?
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, it compiles and runs, but when I get to the part where it should print out the list, the program ends.
This is what is looks like in the console. No errors or anything.

Enter VIN:
12345
Enter a Make:
ford
Enter a year:
2002
Enter mileage:
134000
Enter a price:
5000
Enter another car? (Yes/No)
No
List of Used Cars
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change toand see
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahha! So now I get

Enter VIN:
12345
Enter a Make:
ford
Enter a year:
2002
Enter mileage:
123000
Enter a price:
500
usedCars.UsedCarException: Wrong info entered
Enter another car? (Yes/No)
No
List of Used Cars

which is good because I know something is wrong. But bad because my homework instructions say to use .getMessage()...

also, the other bad news is that I've entered all the info "correctly"... but an exception is still being thrown...
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ford, Honda i.e. the if condition that you have applied falls under approved list or unapproved list?
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest remove the throw( new UsedCarException(vinNum)); first and use System.out.print instead. Go through line by line and check where the error came from.
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Ford, Honda i.e. the if condition that you have applied falls under approved list or unapproved list?


ah, well I feel silly. Those are the approved makes... I had it set up so those would throw the exception. whoops. the whole thing works significantly better now. Thank you so much!
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought, you were raising exception when those conditions are true.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about exception.getMessage() does it work?
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm... it doesn't appear to
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't allow the used car to be printed, but the message doesn't show up in the console
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But! if I do this

it does work...which I think counts as using the homework instructions properly
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should it be??
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS!! I replied bit late, you already figured it out, and it should work in that way only.  getMessage returns a string, so you need to print this string to console.
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking it would work like that eclipse auto made for me in the try/catch.
Thank you so much for your help!
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your constructor in the Exception class doesn't follow the usual format for constructors. You usually take a String which is a message and pass them to the superclass't constructor.
Your line 16 looks incorrect. You are simply calling the get method and doing nothing with it. Just as well, because that field hasn't been initialised at that point, so it will return null. I don't think (not certain) that your constructor will ever set the VIN number. Similarly for the other fields.
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As soon as I had the other problems here worked out I realized my program was only returning null. I managed to get that part working correctly by fixing my constructor and adding a toString Thanks!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!