如图,打印这个值是一个null值,比较却false
import java.net.*;
import java.io.*;
//客户端
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.100",8888);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
for (int x = 0;x < 3 ;x++ )
{
String line = br.readLine();
pw.println(line);
String str = bufIn.readLine();
System.out.println(str);
if (str.equals("欢迎"))
break;
}
s.close();
}
}
//服务端
class LoginServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while (true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
//服务端User线程
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s = s;
}
public void run()
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......connect");
try
{
for (int x = 0;x < 3;x++)
{
BufferedReader br = new BufferedReader(new FileReader("123.txt"));
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String str = bufIn.readLine();
System.out.println(str);
System.out.println(str==null);
System.out.println(str=="null");
String line;
boolean flag = false;
while((line=br.readLine())!=null)
{
if(line.equals(str))
{
flag = true;
break;
}
}
if (flag)
{
System.out.println(str+"......上线了");
pw.println("欢迎");
break;
}
else
{
System.out.println(str+".....尝试上线");
pw.println("用户名不存在");
}
}
s.close();
}
catch (Exception e)
{
}
}
}
------解决思路----------------------
System.out.println(str);输出false,说明要么是空指针,要么是“null”;
System.out.println(str==null);输出false,说明不是空指针,那就只有字符串了
System.out.println(str=="null");输出false,说明“null”是new出来的,而不是直接指向字符串常量。
------解决思路----------------------
System.out.println(str=="null");输出false,字符串怎么可以直等?
------解决思路----------------------
很明显str就是 “null”字符串 system.out.printlin(str.equals("null"))肯定是 true
------解决思路----------------------
感觉这个说的有理哦,试试"null".equals(str)