• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList index out of bounds exception  RSS feed

 
Warren Lucero
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am stuck, I have been working this assignment so long I cannot see the forest for the trees. I have ran the debugger but I am not understanding the error message. I also have a Auto, Customer,and Rental class that WreckDriver calls back to. Any help would be appreciated.



Console output:

Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)

Which customer?
1

The folowing cars are available to rent...
1: a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2: a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3: a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4: a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9

Which Auto:
2

How many days do you wish to have this beautiful vehicle?
5
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at wluceromod5.WreckDriver.main(WreckDriver.java:43)
 
Knute Snortum
Sheriff
Posts: 3545
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to do with this code...

...but you are trying to get an index of dayNum (in this case 5) from r which is an ArrayList that is empty.
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am suppose to ask many days do you want to rent the car for. After entering the number of days, it should calculate the cost based on the number of days as well as a 10% discount for 6 or more days, and additional 15% if you are a gold card member. See other classes below.







 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The final output should look like this:

How many days do you wish to have this beautiful vehicle? 15
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was $191.25
This person rented over six days and received a 15% discount

More rentals (true/false)? false

Rental summary:
Bruce Springsteen (custID: 101) rented a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51 for 10 days.
The cost was $112.50
This person was both a gold card member and rented over six days and received a a 25% discount
Peyton Manning (custID: 103) rented a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9 for 1 days.
The cost was $13.50
This person was a gold card member and received a a 10% discount
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was $191.25
This person rented over six days and received a 15% discount
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Lucero wrote:I am suppose to ask many days do you want to rent the car for. After entering the number of days, it should calculate the cost based on the number of days as well as a 10% discount for 6 or more days, and additional 15% if you are a gold card member.

Yes, but none of that has any relation to the problem you're having, which - as Knute tried to point out - is that you're asking for the 6th element from an ArrayList of Rental objects that is EMPTY.

Winston
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you all are saying now. I do not want to ask for element from the arraylist. Working on the fix now.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original question has already been answered by other posters, however you have numerous issues in the code you have written so far. If you had broken the project down into smaller tasks and then tested along the way, then you would of already found them by now.
For example your method to calculate the rental cost doesn't take in any arguments so how does it know which customer and auto it is calculating the cost for ?
Your COST_PER_DAY constant should be static and then you dont need a getCOST_PER_DAY method
Your test for day discount starts at 7 days and not 6
There are more issues but I'll let you re-evaluate your code before point out anything else.

Keep on asking questions and posting your work and you'll get the help needed.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Browne wrote:. . . Your COST_PER_DAY constant should be static . . .
Why? Won't cost vary from car to car, and won't it be a variable?

Why has the Rental class got an empty constructor?
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The professor pre-populated that empty constructor.
 
Liutauras Vilda
Marshal
Posts: 3805
196
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Lucero wrote:
One tinny thing. You avoided logical error this time, but next time could be not that successful if you ever manage to write:
Then you would end up in the infinity loop, probably more right - you wouldn't end up at all

Just use:
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Liutauras . I did correct but a few minutes before I saw your message. The professor gives us half the code so we can work out the rest. Right now I am stuck on getting the days entered and calculated based three criteria: less then 6 days no discount, over 6 days 15% discount, and extra 10% if a gold card member.
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I am lost. Trying to resolve the entering days and passing it back over to Rental to be calculated then pick the switch string that fits the criteria of days. This is what I started on the WreckDriver class

Here is the Rental class.



I learn faster by seeing examples. Not looking for you all to do my homework. Anyone's guidance would truly appreciated. This assignment is due by tomorrow at noon.
 
Liutauras Vilda
Marshal
Posts: 3805
196
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
less then 6 days no discount, over 6 days 15% discount, and extra 10% if a gold card member.
Forget Java. Probably we you don't have time for sermon, but you really need to solve that first on paper.
As you see you wrote lots of code, even more of it is commented out - and no result basically.

Write down in English what steps you'd take to get that logic which is in quote?
 
Liutauras Vilda
Marshal
Posts: 3805
196
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On technical part, discount shouldn't be a String, i'd think about boolean.
goldCardMember = true / false

After you come up with logic, create method, don't cram everything to whenever you just choose.
It should be similar to (syntax not valid):


There are many ways to polish that, by spending more time on choosing variable names than I did, thinking better about the structure, but basic idea could be like that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!