• 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 solve Array out of index error

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

Having a problem with this program where it won't print out the what day the lowest temperature was on, I get arrayindexoutofbounds error, can't for the life of me figure out why, the error occurs on line 122.



Here an image of the error message, any help appreciated.

[url=]imgur[/url]
 
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the text of the error messages, not a screenshot, which is very difficult to read. That link should tell you how to copy'n'paste the error message.
That isn't an Error, but an Exception. You get out of bounds exceptions if you try to use non‑existent indices. If you have a ten‑element array and try to use myArray[10] or myArray[-1] (‍), you will have such an Exception thrown. The same applies to Lists. If you have a List with seven elements, then accessing myList.get(7) won't work. Indices for days of the week run 0...6 inclusive, not 0...7 inclusive. If you count the tokens in line 116, you will see you have eight array access expressions.
The same indices apply to Lists and arrays.

You might do better to use a loop to iterate your array: much more concise than writing out the elements individually. And, if you get the loop right, much more error‑resistant.
Don't write new XYZ[7] or similar. Declare a local constant DAYS_IN_WEEK and use that throughout. That will ensure that all your arrays are the same size. Mixing arrays length 7 and arrays length 8 is a sure‑fire recipe for mismatches.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding a System.out.printlin of what k is just before you use it.  It's probably not what you think it is...
 
reply
    Bookmark Topic Watch Topic
  • New Topic