我遇到了java中的私人消息問題。我們在啓動客戶端時詢問用戶「名稱」,它是ChatClient.java文件中的變量。我已經做到了「公開」,但是當我嘗試在ChatServer.java中調用它時,它說「找不到符號」。有什麼幫助嗎?同樣在這個片段中,我正在尋找用戶的正確「名字」,是否有任何不符合「我」的名字? (如i.name或當前客戶端與其名稱之間的某種連接)。Java私人消息
的ChatServer問題的一部分
public void sendPrivate(String message) throws Exception {
Iterator<Socket> i = clients.iterator();
StringTokenizer st = new StringTokenizer(message.substring(7,message.length()),", ");
String realMessage = st.nextToken();
String to = st.nextToken();
while (i.hasNext()) {
Socket socket = (Socket) i.next();
try {
if (name.equals(to)){
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages to the client
out.writeUTF(realMessage); // send message to the client
}
}
catch (Exception e) {
System.err.println("[system] could not send message to a client");
e.printStackTrace(System.err);
}
}
}
ChatClient部分在用戶輸入自己的「名字」
public class ChatClient extends Thread
{
public String name;
protected int serverPort = 8888;
public static void main(String[] args) throws Exception {
new ChatClient();
}
public ChatClient() throws Exception {
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
//whitelistaned
String[] whitelist = {"Bob", "Frank", "Goliath", "Zealot", "Bruce Wayne"};
int aliUstrezas = 0;
//common courtesy
System.out.println();
System.out.println("Would you fancy in telling me your name, sir ?");
System.out.println("Current whitelist: {'Bob', 'Frank', 'Goliath', 'Zealot', 'Bruce Wayne'} ");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
請問您是否可以提供確切的錯誤信息,並將其添加到問題本身 – piyushj