0

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()
2
  • 3
    and your question is...? Commented Jan 30, 2012 at 22:17
  • Please check the resulting indentation and other formatting of code samples you post. (I fixed this one up already.) Commented Jan 30, 2012 at 22:21

1 Answer 1

4

split swallows the separator, so you need to change this:

            if("name:".equals(temp[0])){

to this:

            if("name".equals(temp[0])){

Also, this:

                bc(name,line); // method  of outer class - send messages to all

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.