user833129
user833129

Reputation:

Why is the size of this vector 1?

When I use System.out.println to show the size of a vector after calling the following method then it shows 1 although it should show 2 because the String parameter is "7455573;photo41.png;photo42.png" .

private void getIdClientAndPhotonames(String csvClientPhotos)
{
    Vector vListPhotosOfClient = new Vector();
    String chainePhotos = "";
    String photoName = "";
    String photoDirectory = new String(csvClientPhotos.substring(0, csvClientPhotos.indexOf(';')));

    chainePhotos = csvClientPhotos.substring(csvClientPhotos.indexOf(';')+1);
    chainePhotos = chainePhotos.substring(0, chainePhotos.lastIndexOf(';'));
    if (chainePhotos.indexOf(';') == -1)
    {
        vListPhotosOfClient.addElement(new String(chainePhotos));
    }
    else    // aaa;bbb;...
    {
        for (int i = 0 ; i < chainePhotos.length() ; i++)
        {
            if (chainePhotos.charAt(i) == ';')
            {
                vListPhotosOfClient.addElement(new String(photoName));
                photoName = "";
                continue;
            }
            photoName = photoName.concat(String.valueOf(chainePhotos.charAt(i)));
        }
    }
}

So the vector should contain the two String photo41.png and photo42.png , but when I print the vector content I get only photo41.png.

So what is wrong in my code ?

Upvotes: 2

Views: 148

Answers (4)

ZenMaster
ZenMaster

Reputation: 12742

These two lines are the problem:

chainePhotos = csvClientPhotos.substring(csvClientPhotos.indexOf(';') + 1);
chainePhotos = chainePhotos.substring(0, chainePhotos.lastIndexOf(';'));

After the first one the chainePhotos contains "photo41.png;photo42.png", but the second one makes it photo41.png - which trigers the if an ends the method with only one element in the vector.

EDITED: what a mess.

  1. I ran it with correct input (as provided by the OP) and made a comment above.
  2. I then fixed it as suggested above, while accidently changing the input to 7455573;photo41.png;photo42.png; which worked, but is probably incorrect and doesn't match the explanation above input-wise.

I wish someone would un-answer this.

Upvotes: 1

bharath
bharath

Reputation: 14453

You can split the string manually. If the string having the ; symbol means why you can do like this? just do like this,

private void getIdClientAndPhotonames(String csvClientPhotos)
{
    Vector vListPhotosOfClient = split(csvClientPhotos);
}

private vector split(String original) {
Vector nodes = new Vector();
String separator = ";";
// Parse nodes into vector
int index = original.indexOf(separator);
while(index>=0) {
nodes.addElement( original.substring(0, index) );
original = original.substring(index+separator.length());
index = original.indexOf(separator);
}
// Get the last node
nodes.addElement( original );
return nodes;
}

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881323

You have two immediate problems.

The first is with your initial manipulation of the string. The two lines:

chainePhotos = csvClientPhotos.substring(csvClientPhotos.indexOf(';')+1);
chainePhotos = chainePhotos.substring(0, chainePhotos.lastIndexOf(';'));

when applied to 7455573;photo41.png;photo42.png will end up giving you photo41.png.

That's because the first line removes everything up to the first ; (7455573;) and the second strips off everything from the final ; onwards (;photo42.png). If your intent is to just get rid of the 7455573; bit, you don't need the second line.

Note that fixing this issue alone will not solve all your ills, you still need one more change.

Even though your input string (to the loop) is the correct photo41.png;photo42.png, you still only add an item to the vector each time you encounter a delimiting ;. There is no such delimiter at the end of that string, meaning that the final item won't be added.

You can fix this by putting the following immediately after the for loop:

if (! photoName.equals(""))
    vListPhotosOfClient.addElement(new String(photoName));

which will catch the case of the final name not being terminated with the ;.

Upvotes: 4

Andreas Dolk
Andreas Dolk

Reputation: 114767

The answer is not valid for this question anymore, because it has been retagged to java-me. Still true if it was Java (like in the beginning): use String#split if you need to handle csv files.


It's be far easier to split the string:

 String[] parts = csvClientPhotos.split(";");

This will give a string array:

 {"7455573","photo41.png","photo42.png"}

Then you'd simply copy parts[1] and parts[2] to your vector.

Upvotes: 4

Related Questions