How do I use a StringBuffer as an array?

hello!

new problem today. I have a StringBuffer named protocols and I want to go trough it and depending on what it contains to display some Strings.

Here is the code:

...

protocols =new StringBuffer("ITUCAR");

....

for (int i=0;i<protocols.length();i++){

if (protocols[i].toString()==""){

textArea.append("");

}

elseif (protocols[i]=="I"){

textArea.append("IP"+"\n");

}

elseif (protocols[i]=="T"){

textArea.append("TCP"+"\n");

}elseif (protocols[i]=="U"){

textArea.append("UDP"+"\n");

}elseif (protocols[i]=="C"){

textArea.append("ICMP"+"\n");

}elseif(protocols[i]=="A"){

textArea.append("ARP"+"\n");

}elseif (protocols[i]=="R"){

textArea.append("RARP"+"\n");

}

}

I get an error saying "array required but StringBuffer found! :(...

what am I doing wrong?

thank you for lending a helping hand!

Cristina//half of month more till diploma :(>

[2511 byte] By [Cris.Mariaa] at [2007-11-27 4:34:04]
# 1
StringBuffers are not, and never will be, arrays. You might be after a StringTokenizer, though
georgemca at 2007-7-12 9:43:58 > top of Java-index,Java Essentials,Java Programming...
# 2

for (int i = 0; i < protocols.length(); i++) {

switch (protocols.charAt(i)) {

case 'I':

textArea.append("IP"+"\n");

break;

case 'T':

textArea.append("TCP"+"\n");

break;

case 'U':

textArea.append("UDP"+"\n");

break;

case 'C':

textArea.append("ICMP"+"\n");

break;

case 'A':

textArea.append("ARP"+"\n");

break;

case 'R':

textArea.append("RARP"+"\n");

break;

default:

// throw an exception or something

}

}

}

By the way, never compare strings using ==.

uncle_alicea at 2007-7-12 9:43:58 > top of Java-index,Java Essentials,Java Programming...
# 3

hei!

Can u give me more details? how exactly can I use this StringTokenizer on my StringBuffer and what exactly does it do? does it allow me to take each character of the StringBuffer and compare it to other characters like what I have in my code? I need to do something similar to what I have written in my code...check what characters has StringBuffer protocols and display some Strings.

Thank you for your time!

Cristina//looking fwd to pass this bugg

Cris.Mariaa at 2007-7-12 9:43:58 > top of Java-index,Java Essentials,Java Programming...
# 4
hei!thank you uncle_alice! it works now :)!!!and promise I will always use equals on Strings :P...dunno what I was thinking :DCris//with the head in the clouds today :P
Cris.Mariaa at 2007-7-12 9:43:58 > top of Java-index,Java Essentials,Java Programming...