• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

csv file to arraylist

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am writing a function that loads up data from a csv file and poppulates the data in an array list, the data from the csv file is in this format "PLAYER, TEAM, STAT1....STATN"
Now my current function looks like this:

This seems to work fine, the output is as follows: [" Bailey, H", OAK, stat1... statn]\n[" Anderson, T", CWS, stat1... statn]\n... . This is the correct output I need; but I want to change the arraylist variable to be a public static so I want my code to look something like this:

But this gives an incorrect output; the output looks like this: ["Anderdon, T", CWS,stat1...statn][" Bailey, H", OAK, stat1... statn]...
                                                                                       ["Anderdon, T", CWS,stat1...statn][" Bailey, H", OAK, stat1... statn]...

As you can see the fields are being duplicated. Can someone please explain why??

This is my load file function just in case:

Im not sure where the error is

Thanks!
 
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear to have forgotten how the code button works. But this time, I'll correct it for you and your code will look a lot better

moe ham wrote:Hello, I am writing a function that loads up data from a csv file and poppulates the data in an array list . . .

Why do you want a List? Why don't you have a Player class instead, and you can create a Player object for each line.

. . . I want to change the arraylist variable to be a public static . . .

That would be very bad design. Making a field public, especially something mutable like a List, is inviting trouble because other code can modify its state without your knowing about it. And making a field static means there is only one instance of it, so you are going to add all your players to the same List.

. . . This is my load file function just in case:
. . .

Have you been taught to read a file like that? Don't close the file like that; use try with resources instead. Actually I wouldn't use a File reference nowadays.In which case I am assuming that you are processing all those lines in the Player constructor. I haven't allowed for any exceptions that might be thrown because of a malformed line in your file. There are all sorts of alternatives, including:-
  • 1: Create the List with the lines() method of the buffered reader. See below.
  • 2: Give the Player class a factory method taking a line from your csv file as a parameter.
  • How can you get a split String being null? I don't think you can. You can get an empty String: don't test for text.length() == 0 but for text.isEmpty().
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic