aspose file tools*
The moose likes Beginning Java and the fly likes reading a user chosen file into array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading a user chosen file into array" Watch "reading a user chosen file into array" New topic
Author

reading a user chosen file into array

Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
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


~ Jeff Yan ~
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Read the data into ArrayList and then convert the ArrayList into Array


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
il try that and get back to you! cheers mate
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Sep 19, 2008
Posts: 385
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

Joined: Nov 05, 2009
Posts: 42
cheers guys! problem solved!! just got to implement the sorting algorithms now and its done. thanks for your help x
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading a user chosen file into array