免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5786 | 回复: 3
打印 上一主题 下一主题

求助:java readLine()接收到的字符问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-19 10:57 |只看该作者 |倒序浏览
求助:此if (clientin=="123" 条件始终未满足。readLine()接受到的字符除client端发出的"123"字符外,是不是还有\n之类的控制字符?如何才能实现server端根据client端送的字符而选择不同的条件?

server端:
        public void run() {
                    String clientin=null;
                    String[] clientin1=null;
                    try{
                            String line;
                            BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            PrintWriter os=new PrintWriter(socket.getOutputStream());
                            BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
                            clientin=is.readLine();
                            System.out.println("Client:"+ clientnum +clientin);
                            System.out.println("xxxxxxxxxxxx";
                            System.out.print(clientin);
                            if (clientin=="123"{
                                      String command1=new String("/test/mount1.sh";
                                                      System.out.println(command1);                                
                            }

                                       }catch(Exception e){}
                                 }


client端:

        public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals("Send"){        
                   try{
                                  server1=new Socket("127.0.0.1",567;
                          cout1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(server1.
                                                                getOutputStream())), true);
                          cout1.println("123";        
                          cout1.close();
                          server1.close();
                                }catch(IOException e1){}        
                }
                          }

[ 本帖最后由 manzhigang 于 2008-8-19 11:00 编辑 ]

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
2 [报告]
发表于 2008-08-19 11:00 |只看该作者

回复 #1 manzhigang 的帖子

java中字符串比较需要使用方法equals,不能使用==来比较。
比如:
if (clientin.equals("123"))

论坛徽章:
0
3 [报告]
发表于 2008-08-19 11:14 |只看该作者
感谢happy_fish100  的回复

论坛徽章:
0
4 [报告]
发表于 2008-08-19 11:17 |只看该作者
equals方法是Object类的一个方法,所有继承自Object类的类都会集成此方法,并且可以重载这个方法来实现各自的比较操作,而且jdk也正是推荐这种做法。所以开发人员尽可以在自己的类中实现自己的equals方法来完成自己特定的比较功能,所以各个类的equals方法与= =之间并没有绝对的关系,这要根据各自类中自己的实现情况来看。也就是说可能会有两种情况发生:equals方法和= =相同或者不相同。在多数情况下这两者的区别就是究竟是对对象的引用进行比较还是对对象的值进行比较(其他特殊情况此处不予考虑)。那么= =操作符是比较的什么呢?= =操作符是比较的对象的引用而不是对象的值。并且由下面的源代码可以看出在最初的Object对象中的equals方法是与= =操作符完成功能是相同的。
源码:
java.lang.Object.equals()方法:
-------------------------------------------------------------
public boolean equalss(Object obj) {
return (this = = obj);
}
-------------------------------------------------------------
jdk文档中给出如下解释:
-------------------------------------------------------------
The equalss method implements an equivalence relation:
? It is reflexive: for any reference value x, x.equalss(x) should return true.
? It is symmetric: for any reference values x and y, x.equalss(y) should return true if and only if y.equalss(x) returns true.
? It is transitive: for any reference values x, y, and z, if x.equalss(y) returns true and y.equalss(z) returns true, then x.equalss(z) should return true.
? It is consistent: for any reference values x and y, multiple invocations of x.equalss(y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified.
? For any non-null reference value x, x.equalss(null) should return false.
The equalss method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equals objects must have equals hash codes.
-------------------------------------------------------------
由以上的注释可知equals方法和 = =操作符是完成了相同的比较功能,都是对对象的引用进行了比较。那么我们熟悉的String类的equals方法是对什么内容进行比较的呢?下面我们来看它的代码和注释:
源代码:
-------------------------------------------------------------
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

-------------------------------------------------------------
此方法的注释为:
-------------------------------------------------------------
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
-------------------------------------------------------------
由上面的代码和注释可以得到String类的equal方法是对对象的值进行比较。
根据以上的讨论可以得出结论:equal方法和= =操作符是否存在区别要个别对待,要根据equal的每个实现情况来具体判断。
*******************************

belter(belter@sina.com)

none.blogdriver.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP