substring questions
Hi,
Evertime i start my application "online" is added to the JList but i don't want that, does anyone know whats wrong with this code:
Gui:
new Thread(this).start();
TextMessage tm = ts.createTextMessage();
tm.setText("online :"+usr+":");
tpub.publish(t, tm);
publicvoid setText(String s){
int index1 = s.indexOf("");
int index2 = s.indexOf(":",index1+1);
String nameLocal = s.substring(index1, index2);
if (!(userList.contains(nameLocal))){
userList.add(nameLocal);//arraylist
list.add(nameLocal);//Jlist
textArea.append(nameLocal+" added\n");
}
else{
textArea.append(s+"\n");
textField.setText("");
}
}
I'm thinking that "online" in my Gui is the problem but i cannot filter it out with substrings.
[1499 byte] By [
deAppela] at [2007-11-27 3:48:29]

> int index1 = s.indexOf("");
Here, you're trying to find out the index of a space right? The one that comes after 'online' and before ':' ?
What you've specifed is an empty string, so it matches at the very index. So index1 will always hold 0 after this statement.
> int index2 = s.indexOf(":",index1+1);
And here you're trying to find the last ':', the one that comes after usr? So that you can extract the username? Well, assuming you correct the first error and get the index of the space following 'online', this will give you the index of the first ':'. So you'll end up with index1 = 6 and index2 = 7. This means,
> String nameLocal = s.substring(index1, index2);
nameLocal here will be substring(6, 7) giving you the space after 'online'.
If you're trying to extract the username, this should be your code:
int index1 = s.indexOf(":"); //index of the colon after online
index1++; //increment to hold index of start of usr
int index2 = s.indexOf(":",index1); //index of colon following usr
String nameLocal = s.substring(index1, index2); //get the username