Java分割输入

Java分割输入,java,text-processing,Java,Text Processing,我试图读入一个命令和一个名字。例如“name:”+“username”,我想将用户名添加到arraylist中。我正在尝试拆分输入,因此我有一个name变量和一个username变量,如下所示: public void run() { String line; try { while(true) { line = input.readLine(); String[] temp; temp =

我试图读入一个命令和一个名字。例如“name:”+“username”,我想将用户名添加到arraylist中。我正在尝试拆分输入,因此我有一个name变量和一个username变量,如下所示:

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()

split
会吞下分隔符,因此您需要更改此选项:

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

            if("name".equals(temp[0])){
此外,这:

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

看起来有点奇怪,因为它引用了一个名为
name
的变量,但您发布的代码片段中没有声明该变量,或者(除了这一行)引用了该变量。

split
吞没了分隔符,所以您需要更改:

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

            if("name".equals(temp[0])){
此外,这:

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

似乎有点奇怪,因为它引用了一个名为
name
的变量,但您发布的代码段中没有声明该变量,或者(除了这一行)引用了它。

您的问题是…?请检查您发布的代码示例的缩进和其他格式。(我已经解决了这个问题。)你的问题是…?请检查你发布的代码示例的缩进和其他格式。(我已经把这个修好了。)