java获取拒绝ArrayList中存在的用户名的代码。
在聊天室代码中,我一直在试图找出如何添加一个代码,这样我就可以拒绝已经存在用户名的客户端。我已经创建了一个ArrayList
,在那里我可以存储连接的客户端的用户名。但每次我运行客户机时,它都会接受我以前使用过的名称。这是我的密码
if(obj == login) {
// Need to establish a connection request
String username = tf.getText().trim();
clientList.add(username);
// ignore empty username
if(username.length() == 0 || username.equals("Your name")){
JOptionPane.showMessageDialog(null,"Please enter a valid username","Alert Message",JOptionPane.WARNING_MESSAGE);
return;
}
if(clientList.size() > 1 && clientList.contains(username)){
JOptionPane.showMessageDialog(null,"This username is in use","Alert Message",JOptionPane.WARNING_MESSAGE);
return;
}
// try creating a new Client with GUI
client = new Client(username, this);
// test if we can start the Client
if(!client.start())
return;
//clientList.add(client);
tf.setText("");
tf.setBackground(Color.YELLOW);
label.setText("Enter your message in the yellow box");
connected = true;
login.setEnabled(false);
logout.setEnabled(true);
tf.addActionListener(this);
// Action listener for when the user enter a message
}
需要帮忙吗
共 (0) 个答案