【发布时间】:2014-07-01 03:00:22
【问题描述】:
这是我在使用 Array 和 String 类的 equals() 方法时遇到的一个非常令人困惑的问题:这些方法总是返回 false。我已经测试了空格,调试了几个小时,但我找不到一个修复程序。这是一个加密的服务器聊天室。我只会发布服务器的 sn-p,因为我猜这将是唯一的问题,但如果您希望我发布其他 sn-ps 的代码或类,我会的。 服务器代码sn -p:
private void handlePassword() {
try {
out.println(encrypter.encrypted("Enter Password."));
out.flush();
char[] ep = null;
ep = encrypter.decrypted(in.readLine()).toCharArray();
boolean tr = true;
if (ep.length == password.length)
for (int i = 0; i < ep.length; i++) {
System.out.print(ep[i]);
System.out.print(password[i]);
if (ep[i] == password[i]) {
tr = false;
break;
}
System.out.println();
}
System.out.println("AHHHHHHHHHHHH! " + tr);
System.out.println(String.valueOf(ep) + "\\/"
+ String.valueOf(password));
System.out.println(Arrays.equals(ep, password));
if (!Arrays.equals(ep, password)) {
out.println(encrypter.encrypted("Incorrect Password."));
out.flush();
handlePassword();
} else {
out.println(encrypter.encrypted("Access Granted"));
out.flush();
}
} catch (Exception e) {
reportError(e);
}
}
-变量说明
out = PrintWriter
in = BufferedReader
encrypter = My Encrypting Class
char[] password = The Server Password
-控制台输出
无效时(密码:asdf)(尝试密码:hhhh):
AHHHHHHHHHHHH! true
[asdf] hhhh\/asdf
false
有效时:(密码:asdf)(尝试密码:asdf):
AHHHHHHHHHHHH! true
[asdf] asdf\/asdf
false
我真的看不出一个问题。请解释为什么会这样。我真的说不出来。
【问题讨论】:
-
发布几百行代码确实对您的问题没有帮助。您遇到此问题的原因是什么?
-
您能准确指出您遇到的问题吗?
-
你发布的代码太多了。最好用一个简化的例子来隔离问题。
-
该方法是代码的主要部分!
标签: java client-server