Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to divide more than two numbers using an ArrayList?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! Thanks Pawel Pawlowicz!!
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jelle Klap!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic