• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

While reading stored values from Excel file I'm getting: java.lang. ArrayIndexOutOfBoundsExceptio: 2  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am I getting the Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at population.Population.main(Population.java:80)

error message for my Java code?


I tried removing this part of the code but then it just asks for user input and then the program ends. Any help on this is appreciated. My guess is something is wrong with this logic maybe?




 
Bartender
Posts: 5776
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ash Sadf wrote:

I'd put the results of nextLine() into a String variable and print it to see what it really thinks it is getting and how many commas are in it.
 
Ash Sadf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ash Sadf wrote:

I'd put the results of nextLine() into a String variable and print it to see what it really thinks it is getting and how many commas are in it.



I'm not quite sure what you are trying to say. So you mean output the results of String a[]?
So I did


The program outputs all the data from the excel file.
 
Ash Sadf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It outputs all the data then asks for user input.
If I input something it just ends the program.
 
Carey Brown
Bartender
Posts: 5776
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your print doesn't behave the same way the one I suggested would. Arrays.toString() will gladly print arrays of lengths 0, 1, 2, 3 without barfing. Your exception shows that somewhere you are getting data that has only one comma in it.
 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The program outputs all the data from the excel file.


What was in the last line printed before the exception was thrown?


To see what is happening, add an if statement after the split statement to test if the length of a is less than 3 and print the contents of a if it is.
 
Saloon Keeper
Posts: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The CSV file format has a number of edge cases that your code doesn't handle. Before you're done implementing those, I think it would be faster to use a CSV library like Apache Commons CSV.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!