Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading a user chosen file into array

 
Jeff Yan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically, im having a little bit of coding trouble. i got to read the data from a user chosen file and then output it into an array for an algorithm to sort. the problem is that the 4 test files have different amounts of integers in, so i dont know what the initial size of the array is going to be. and it comes up with a null pointer error on runtime if i dont state the size of the array! the code itself has no syntax errors. any help appreciated. my code is listed below:


many thanks

Jeff
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the data into ArrayList and then convert the ArrayList into Array

 
Jeff Yan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
il try that and get back to you! cheers mate
 
Jeff Yan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, it is coming back with null when i try and output the contents of the array in the terminal window . . . . . this is the code i did to put the elements from the arrayList into the array


not sure if it is right?? the arraylist is definantly full as i did a println for the list.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are putting the contents into the array several times; you only need to do it once. You are also declaring the array inside the for, so it is a local variable which goes out of scope at the end of the block. You must be shadowing a field of the same name, which you are never gaining access to from the "for".
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you do not need loop statement to convert arraylist into array

Use for loop to add data into the arraylist.

Then outside for loop use something like this.



You will be all set.
 
Jeff Yan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers guys! problem solved!! just got to implement the sorting algorithms now and its done. thanks for your help x
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic