I am trying to read in a command and a name. For example “name:” + “username” and I want to add the username to an arraylist. I am trying to split the input, so that I have a name variable and a username variable as shown below:
public void run() {
String line;
try {
while(true) {
line = input.readLine();
String[] temp;
temp = line.split(":");
//checks different input from the client
//checks to see if the client wants to terminate their connection
//removes the client's name from the list
if("name:".equals(temp[0])) {
users.add(temp[1]);
output.println("OK");
}
else {
broadcast(name,line); // method in outer class - send messages to all
}
} // end of while
} catch(Exception e) {
System.out.println(e.getMessage());
}
} // end of run()
splitswallows the separator, so you need to change this:to this:
Also, this:
seems a bit odd, in that it refers to a variable named
name, but nothing in your posted snippet declares that variable, or (aside from this line) refers to it.