java无法比较字符串 [英] java can't compare strings

查看:122
本文介绍了java无法比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个从套接字读取一行的代码。


如果该行是再见,我希望它输出不错。
由于某些原因它不起作用,虽然我确实尝试输出没有条件的输入并且它确实说再见



这是我的代码

  String input = null; 
if(socket.getInputStream()。available()> 0)
input = in.readLine();

if(input!= null)
{
input = input.trim();
if(input ==bye)
out.println(nice);

out.println(输入);
}


解决方案

使用 String#等于() ,而不是 == ,来比较字符串。


i'm written a code that reads a line from the socket.
if the line is "bye" i want it to output "nice". from some reason it doesn't work, although i did try to output the input with no conditions and it did says "bye"

this is my code

                String input = null;
                if (socket.getInputStream().available() > 0)                    
                    input = in.readLine();                                                                  

                if (input != null)
                {
                    input = input.trim();
                    if (input == "bye")
                        out.println("nice");

                    out.println(input);
                }

解决方案

Use String#equals(), not ==, to compare strings.

这篇关于java无法比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆