Win a copy of Java XML & JSON this week in the XML and Related Technologies 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 5459
55
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: 5459
55
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: 5219
143
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!