![](/img/trans.png)
[英]Why should two equal objects return equal hash codes if I don't want to use my object as a key in a hash table?
[英]Why don't the two equal strings match?
我想比較服務器的響應和字符串 ,但是在測試兩個字符串時我得到一個錯誤的結果。 為什么?
我找到了這個但沒有幫助: 我如何比較Java中的字符串?
我試過兩種方法:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if(Objects.equals((code = in.readLine()), "S")) { //Input string: "S"
//code
}
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if((code = in.readLine()).equals("S")) { //Input string: "S"
//code
}
在任何一種情況下代碼都不會運行,因為測試的值為false 。
class ManagePhoneClients
{
public void managePhoneClients(object obj)
{
Boolean socketalive = true;
TcpClient tcpClient = (TcpClient)obj;
StreamReader sr = new StreamReader(tcpClient.GetStream(), Encoding.UTF8);
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
Boolean isPhoneClientConnected = false;
String user;
String answer;
String tl;
List<string> LC = new List<string>();
Boolean qss = false;
Program program = new Program();
Int32 points = 0;
ConsoleMethods.writeLine("Thread started for the phone client.", "Info", ConsoleColor.Cyan);
sw.WriteLine("S");
sw.Flush();
while (socketalive == true)
{
try
{
if (Program.isMainClientConnected != true || Program.isPowerPointConnected != true)
{
ConsoleMethods.writeLine("Connection refused because the necessary clients are not connected!", "Error", ConsoleColor.Red);
sw.WriteLine("NS");
sw.Flush();
tcpClient.Close();
socketalive = false;
}
else
{
sw.WriteLine("LC");
sw.Flush();
}
if (isPhoneClientConnected != true & sr.Peek() != -1)
{
String rLC = sr.ReadLine();
LC.AddRange(rLC.Split('|'));
if (LC[1].ToString() == Program.passPhoneClient)
{
user = LC[0];
Program.userNames.Add(user);
ConsoleMethods.writeLine("Phone connected from: " + tcpClient.Client.RemoteEndPoint, "Info", ConsoleColor.Cyan);
sw.WriteLine("S");
sw.Flush();
Program.utnr = rLC;
isPhoneClientConnected = true;
}
else
{
sw.WriteLine("NS");
sw.Flush();
socketalive = false;
ConsoleMethods.writeLine("Phone client disconnected because the password was invalid!", "Error", ConsoleColor.Red);
}
}
switch (sr.ReadLine())
{
case "CLIENT-EXCEPTION":
ConsoleMethods.writeLine("Exception in phone client from: " + tcpClient.Client.RemoteEndPoint + "\n" + sr.ReadLine(), "Client-Error", ConsoleColor.DarkRed);
break;
case "RECEIVED_POINTS":
int point = int.Parse(sr.ReadLine());
points += point;
ConsoleMethods.writeLine("Phone client succesfully completed a task from: " + tcpClient.Client.RemoteEndPoint + " Point: " + point, "Client-Received Points", ConsoleColor.DarkRed);
ConsoleMethods.writeLine("Phone client collected points from: " + tcpClient.Client.RemoteEndPoint + " Points: " + points, "Client-Collected Points", ConsoleColor.DarkRed);
break;
}
}
catch (Exception e)
{
tcpClient.Close();
socketalive = false;
ConsoleMethods.writeLine(e.Message + e.StackTrace + e.StackTrace, "Error", ConsoleColor.Red);
}
}
}
}
(這還沒有完成!)
public void login(View v) {
final Context context = this;
new Thread(new Runnable() {
public void run() {
try {
final Socket socket = new Socket("192.168.0.104", 90);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print("P" + "\r\n");
out.flush();
String code;
code = in.readLine();
if(code.equals("S")) {
if (Objects.equals((code = in.readLine()), "LC")) {
out.print(((EditText)findViewById(R.id.username)).getText().toString() + "|" + ((EditText)findViewById(R.id.password)).getText().toString() + "\r\n");
out.flush();
if(Objects.equals((code = in.readLine()), "S")) {
new ServerContact(context).Listener(socket);
startActivity(new Intent(Login.this, Waiting.class));
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} catch (Exception e) {
new ExceptionWriter(e);
}
}
}).start();
}
(這還沒有完成!)
我設法解決了它。 在服務器端,我必須禁用BOM。
沒有BOM:
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), new UTF8Encoding(false));
使用BOM:
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
它對我有用(使用你的第一個案例)。 我想我們都已經得出結論, code
實際上並不等於"S"
,抱歉。
public class EqualsTest {
public static void main( String[] args ) throws IOException {
MyStream socket = new MyStream( new ByteArrayInputStream( "S\n".getBytes() ));
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream(), "UTF8" ) );
String code;
if( Objects.equals( (code = in.readLine()), "S" ) ) {
System.out.println( "true" );
} else {
System.out.println( "false" );
}
}
static class MyStream {
private final InputStream ins;
public MyStream( InputStream ins ) {
this.ins = ins;
}
public InputStream getInputStream() {
return ins;
}
}
}
輸出:
run:
true
BUILD SUCCESSFUL (total time: 0 seconds)
我將添加一些測試code
進行調試的想法:
// how to debug
System.err.println( "code="+code+" length="+code.length() );
System.err.println( "code bytes="+Arrays.toString( code.getBytes() ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.