Hi Guys,

I'm basicly reading from a CSV File that im using to store data.

Now when I read from the CSV file i want to load the data to fill in the parameters

eg.
the csv file contains

name,address,age,favColor.

so when i load the file i want it to read in the info and create a new User using the info supplied from the document.

eg.
new Client(name,address,age,favColor).

the main problem is i dont know how long it will be till the next comma?

would it be an idea to load a line.
save the info between commas in temp varibles.
then create the new Client?

Any help much appreciated.

Yes, that would be an idea. If you mean what I think you mean, it would be a good idea. Why don't you try it and see?

i was going to try it but, i was not sure it was the most effecient way so wanted a bit of advice before hand incase there was a better way.

Well, it might not be the most efficient, depending on how you wanted to get the values in between the commas, but you should do it anyway.
There's always a better way to do it, you shouldn't let that stop you from having a go. Get it running, and then look for improvements where they're actually needed. Do this enough, and you'll learn the best ways to write the code, trust me.

If, after you've written something, you think it's just too ugly to live, then you can ask for suggestions for improvements, but you'll learn the most if you work through your own ideas first.

Here is my attempt. it works fine. and not as messy as i thought it would be.

private void LoadRoster(File clients)
    {
Client[] tempClient = new Client[10];
        String[] temp = new String[80];//i know that there are only 80 lines while testing but should really make the method scan the file to find how many lines there are and then make this array the required size.

        try {
            BufferedReader in = new BufferedReader(new FileReader(clients));
            String str;
            clientRoster = new ClientRoster();
            while((str = in.readLine()) != null)
            {
                temp = str.split(",");
            }

            for(int j = 0; j < 80; j+=8)
            {
                Client tempo = new Client(temp[j],Client.Type.valueOf(temp[j+1]),
                        Integer.parseInt(temp[j+2]),Integer.parseInt(temp[j+3]),
                        Integer.parseInt(temp[j+4]),Integer.parseInt(temp[j+5]),
                        Integer.parseInt(temp[j+6]),Integer.parseInt(temp[j+7]));
                clientRoster.AddClient(tempo, FirstSpaceFree());
            }
        } catch (IOException ie) {
        }
        UpdateRoster();
        this.repaint();
    }

Good work. Way to stick it!
The split method is in fact the best way I can think of to handle this sort of problem - I didn't bring it up because I figure everyone should parse a comma-separated line at least once, so they know why split() exists.

The only thing I'm not sure about is this:

while((str = in.readLine()) != null)
            {
                temp = str.split(",");
            }

It looks to me like this ends up with just the last line of your input data split into temp.
Are you sure you don't want something more like

while((str = in.readLine()) != null)
            {
                temp = str.split(",");
            
                Client tempo = new Client(temp[0],Client.Type.valueOf(temp[1]),
                        Integer.parseInt(temp[2]),Integer.parseInt(temp[3]),
                        Integer.parseInt(temp[4]),Integer.parseInt(temp[5]),
                        Integer.parseInt(temp[6]),Integer.parseInt(temp[7]));
                clientRoster.AddClient(tempo, FirstSpaceFree());
            }
        } catch (IOException ie) {
        }

This would take each line, split it, and make a Client of the result.

because that will only add the last client not all the clients to the array.
Or thats what happend when i tried it.

The while loop takes the whole document word by word and adds it to the array not just the last line.

oh and i love split() use it all the time.

Well, if it works for you. It looks to me like it's the other way around, but it's your code.

Oh, maybe your data file doesn't have line breaks - in that case, it would work the way you describe.

If you want to check, put in a counter in the while loop - something as simple as

        while((str = in.readLine()) != null)
        {
            temp = str.split(",");
            System.out.print("*") 
        }

To see how many times it's going through that loop. I wouldn't be surprised if you're only going around that loop once.

no the data file does not have any line breaks in it.
so that explains it all.

commented: I meant to add rep on the previous post, so here it is. +2

That presents scalability problems. You might want to look at that if you intend to pursue this one further. If you're generating the data files, it's easy enough to fix - and then you'll have to go back and look at this class, because that fix will break what you've done here. Fortunately, you have a fix in hand. :)

Anyway, good work - you knocked that one clean in one shot, without anything but a suggestion that you could probably do it. That's good programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.