This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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?




 
Saloon Keeper
Posts: 5144
54
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
Saloon Keeper
Posts: 5144
54
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: 3316
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: 5040
134
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!