Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建新RMI聊天室时出现迭代器逻辑问题_Java_Logic - Fatal编程技术网

Java 创建新RMI聊天室时出现迭代器逻辑问题

Java 创建新RMI聊天室时出现迭代器逻辑问题,java,logic,Java,Logic,我在一个研究项目上工作了很长时间,我认为阻碍我前进的一个因素是不知道如何正确地为一些代码执行循环逻辑,这些代码旨在创建一个新的聊天室,或者将用户连接到现有的聊天室 此代码似乎在每次有人连接时创建一个新房间,即使房间名相同。我很确定这种方法是错误的,但不确定从哪里开始纠正。 任何帮助都会很好。即使是建议一种不同的方法 public void connect(RMIChatClient theClient, String roomName) throws RemoteExce

我在一个研究项目上工作了很长时间,我认为阻碍我前进的一个因素是不知道如何正确地为一些代码执行循环逻辑,这些代码旨在创建一个新的聊天室,或者将用户连接到现有的聊天室

此代码似乎在每次有人连接时创建一个新房间,即使房间名相同。我很确定这种方法是错误的,但不确定从哪里开始纠正。 任何帮助都会很好。即使是建议一种不同的方法

    public void connect(RMIChatClient theClient, String roomName)
        throws RemoteException {
    // check if room exists and pass the client to the relevant room
    // cycle list of rooms
    Iterator<RMIRoomImpl> it = myRoomsList.iterator();
    while (it.hasNext()) {
        RMIRoomImpl roomTemp = (RMIRoomImpl) it.next();
        if (roomTemp.getName() == roomName) {
            //if there is a match then just add the client to the room
            roomTemp.addClient(theClient);
            System.out.println("Bound Client: " + theClient + "in Existing Room:"
                    + roomName);
            match = true;
            return;
        }

    }
    if (match != true) {
        // if there is no match then create a new room and pass the first client
        RMIRoomImpl newRoom =  new RMIRoomImpl(roomName, theClient);
        System.out.println("new room created: " + roomName);
        myRoomsList.add(newRoom);
        System.out.println("Bound Client: " + theClient + "in Room:"
                + roomName);
    }

}
public void connect(RMIChatClient-theClient,String-roomName)
抛出远程异常{
//检查房间是否存在,并将客户交给相关房间
//循环房间清单
Iterator it=myRoomsList.Iterator();
while(it.hasNext()){
RMIRoomImpl roomTemp=(RMIRoomImpl)it.next();
if(roomTemp.getName()==roomName){
//如果存在匹配项,则只需将客户添加到房间
roomTemp.addClient(客户机);
System.out.println(“绑定客户端:+现有房间中的客户端+”:
+房间名称);
匹配=真;
返回;
}
}
如果(匹配!=真){
//如果没有匹配项,则创建一个新房间并传递第一个客户
RMIRoomImpl newRoom=新RMIRoomImpl(房间名称,客户);
System.out.println(“创建的新房间:“+roomName”);
myRoomsList.add(新建房间);
System.out.println(“绑定客户机:+客户机+”房间内:
+房间名称);
}
}
您有:

if (roomTemp.getName() == roomName) 
你的意思是:

if (roomTemp.getName().equals(roomName))

如果名称不区分大小写,则使用。如果前导/尾随空格存在问题,请使用“第一”

顺便说一句,如果一个
RMIRoomImpl
的名字从未改变,你可能会发现一个
Map
更方便。

可能是