• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to divide more than two numbers using an ArrayList?  RSS feed

 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I made a calculator that does the basic operations but I want to be able to divide more than two numbers by repeatedly entering values using an arraylist and I keep getting the indexoutofboundsexception and I don't know why? I understand the reason behind the outofboundsexception but I can't figure out why in this case. Please excuse me, its my first time here. Any help is appreciated. Here is a snippet:
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is with numList.get(i+1).
You iterate i variable from 0 to numList.size()-1.
When you invoke numList.get with value numList.size() (calculated from numList.size()-1 + 1) you get the exception.
 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thanks Pawel Pawlowicz!!
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, and welcome to JavaRanch!
Kudos for using code-tags in your first post, but you didn't actually place the code inside them, so I went ahead and fixed that for you

Now for the problem, one source of a possible IndexOutOfBoundsException is the get() call at line 3 if the list is empty.
Two other possible sources can be found inside the body of the for-loop when it reaches the last iteration.

Ps. I'm assuming that list and numList actually should be the same reference and this is a copy-paste error?

Edit: Awww, too late.
 
Jeffery Colins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jelle Klap!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!