vector not storing objects properly,

hi,

i'm reading a file using a stringtokenizer, to parse it, and storing the read content into a vector, but when i display the vector's contents, they seem to be out of place. here is the code:

import java.io.*;

import java.util.*;

class ReadFile{

publicstaticvoid main(String args[]){

Vector programs =new Vector(4);

programs.add(0,null);

programs.add(1,null);

programs.add(2,null);

programs.add(3,null);

String text;

String programLink;

String programName;

try{

BufferedReader br =new BufferedReader(new FileReader("progList.txt"));

text = br.readLine();

while (text !=null){

StringTokenizer strToken =new StringTokenizer(text,",");

programLink = strToken.nextToken();

System.out.println(programLink);

programName = strToken.nextToken();

System.out.println(programName);

if(programName.equals("Microsoft Word")){

programs.add(0,programLink);

}

if(programName.equals("Microsoft Excel")){

programs.add(1,programLink);

}

if(programName.equals("Microsoft Outlook")){

programs.add(2,programLink);

}

if(programName.equals("Microsoft PowerPoint")){

programs.add(3,programLink);

}

text = br.readLine();

}

}catch(FileNotFoundException e){

System.out.println("File not found: " + e);

}catch(IOException e){

System.out.println("IOException found: " + e);

}

for(int i = 0; i < 4; i++){

System.out.println("Item at location " + i +" is " + (String)programs.elementAt(i));

}

}

}

the file looks like:

C:\WIN98_SE\start menu\programs\Acrobat Reader 5.0.lnk,Acrobat Reader 5.0

C:\WIN98_SE\start menu\programs\Microsoft Excel.lnk,Microsoft Excel

C:\WIN98_SE\start menu\programs\Microsoft Outlook.lnk,Microsoft Outlook

C:\WIN98_SE\start menu\programs\Microsoft Word.lnk,Microsoft Word

C:\WIN98_SE\start menu\programs\Microsoft PowerPoint.lnk,Microsoft PowerPoint

C:\WIN98_SE\start menu\programs\MSN Messenger.lnk,MSN Messenger

if you run it you'll see that it adds Word and PowerPoint ok, but not the others, but i can't figure out where it's storing them incorrectly.

Thanks.

[3974 byte] By [soni29] at [2007-9-27 22:06:26]
# 1

The way you're using a Vector is more like an array and isn't using Vector's dynamic sizing (which is why you would use a vector). Actually, it is using dynamic resizing, but I don't think that's what you want.

Vector.add() does an insert, not a replace, so items will be bumped ahead in the vector, which is why you have things out of order or in the wrong place.

bass345 at 2007-7-7 12:07:11 > top of Java-index,Archived Forums,Java Programming...
# 2

> Vector.add() does an insert, not a replace, so items

> will be bumped ahead in the vector, which is why you

> have things out of order or in the wrong place.

yep. that's the problem. it adds Excel at location 1, Outlook at location 2, then Word at location 0 (this pushes everything down one place - null at location 1, Excel is now at location 2, Outlook is now at location 3), then it adds PowerPoint at location 3. So your vector looks like

1. Word

2. null

3. Excel

4. Powerpoint

5. Outlook

Jetset_Willy at 2007-7-7 12:07:11 > top of Java-index,Archived Forums,Java Programming...
# 3

hi,

thanks for the response, i realize that i'm using it more like an array, but i justed to try it out, i tried changing the .add to .insertAt giving it the location, but still no luck. here is what it looks like now:

import java.io.*;

import java.util.*;

class ReadFile {

public static void main(String args[]) {

Vector programs = new Vector(4);

programs.add(0, null);

programs.add(1, null);

programs.add(2, null);

programs.add(3, null);

String text;

String programLink;

String programName;

try {

BufferedReader br = new BufferedReader(new FileReader("progList.txt"));

text = br.readLine();

while (text != null) {

StringTokenizer strToken = new StringTokenizer(text, ",");

programLink = strToken.nextToken();

System.out.println(programLink);

programName = strToken.nextToken();

System.out.println(programName);

if(programName.equals("Microsoft Word")) {

programs.insertElementAt(programLink, 0);

}

if(programName.equals("Microsoft Excel")) {

programs.insertElementAt(programLink, 1);

}

if(programName.equals("Microsoft Outlook")) {

programs.insertElementAt(programLink, 2);

}

if(programName.equals("Microsoft PowerPoint")){

programs.insertElementAt(programLink, 3);

}

text = br.readLine();

}

}catch(FileNotFoundException e) {

System.out.println("File not found: " + e);

}catch(IOException e) {

System.out.println("IOException found: " + e);

}

for(int i = 0; i < 4; i++) {

System.out.println("Item at location " + i + " is " + (String)programs.elementAt(i));

}

}

}

soni29 at 2007-7-7 12:07:11 > top of Java-index,Archived Forums,Java Programming...
# 4
thanks. i figured it out, using setElementAt worked. Thanks again.
soni29 at 2007-7-7 12:07:11 > top of Java-index,Archived Forums,Java Programming...
# 5
Try adding 'tics' around program name to check for possible extraneous spaces.System.out.println("programName = '" + programName +"'");
javafarmhand at 2007-7-7 12:07:11 > top of Java-index,Archived Forums,Java Programming...