明明应该一样的数据,却判断为不一样,实在想不通,怎样能判断成一样吗
代码如下:
/*********InputStreamExample.java文件*********/
import java.net.*;
import java.io.*;
public class InputStreamExample {    public InputStreamExample() {}
    public static void main (String[] args) {
     try{
     URL url=new URL("http://www.uni-card.net/updateV2.php?usernum=15904060643&tel2=15904060643&qnck=03261099659903239&qncpw=829516006350133025");
     try{
     URLConnection c=url.openConnection();
     String txtContent="";
     InputStream is;
     int ch;
     try{
     is=c.getInputStream();
     ch=is.read();
     while (ch!=-1){
     txtContent=txtContent+(char)ch;
     ch=is.read();
     }
     is.close();
     }catch(IOException io1){
     System.out.println("IOException异常: "+io1.toString());
     }
                        //下载网页代码
                        txtContent=new String(txtContent.getBytes("8859_1"),"utf-8");
     //txtContent=new String(txtContent.getBytes("8859_1"));//乱码
     //提取数据
     int pos1=txtContent.indexOf("<title");
                        int pos2=txtContent.indexOf("</title>");
                        txtContent=txtContent.substring(pos1,pos2);
                        //验证是否一致
                        /*if(txtContent.Equals("<title>优尼->提示页面")==true){
                                                  System.out.println("一样");
                                                }else{
                                                  System.out.println("不一样");
                                                }*/     System.out.println(txtContent);//输出要判断的数据
                        if(txtContent=="<title>优尼->提示页面"){
                          System.out.println("一样");
                        }else{
                          System.out.println("不一样");
                        }
                
     }catch(IOException io2){
     System.out.println("IOException异常: "+io2.toString());
     }            }catch(MalformedURLException e){
              System.out.println("MalformedURLException异常:"+e.toString());
            }    }
}

解决方案 »

  1.   

    另外,你可以debug去看看你得到的txtContent的值到底是多少,有没有可能不一样,尤其是多个空格少个空格之类。
      

  2.   

    txtContent是“<title>优尼->提示页面”
      

  3.   

    验证对象是否相同要用equals方法,==是不能够用来验证对象是否一致的。
    不过在调用equals方法前必须验证调用equals的方法不能为null
      

  4.   

    如果可以,你最好debug进equals那个方法内部去看看。
      

  5.   

    我注释的那个equals是不是写的不对啊
      

  6.   

    用equals后调试报错

    --------------------配置: <默认>--------------------
    D:\Test\InputStreamExample.java:40: 找不到符号
    符号: 方法 Equals(java.lang.String)
    位置: 类 java.lang.String
                            if(txtContent.Equals("<title>优尼->提示页面")==true){
                                         ^
    1 错误处理已完成。
      

  7.   

    package com.huateng.test;import java.net.*;
    import java.io.*;public class InputStreamExample { public InputStreamExample() {
    } public static void main(String[] args) {
    try {
    URL url = new URL("http://www.uni-card.net/updateV2.php?usernum=15904060643&tel2=15904060643&qnck=03261099659903239&qncpw=829516006350133025");
    try {
    URLConnection c = url.openConnection();
    String txtContent = "";
    InputStream is;
    int ch;
    try {
    is = c.getInputStream();
    ch = is.read();
    while (ch != -1) {
    txtContent = txtContent + (char) ch;
    ch = is.read();
    }
    is.close();
    } catch (IOException io1) {
    System.out.println("IOException异常: " + io1.toString());
    }
    // 下载网页代码
    txtContent = new String(txtContent.getBytes("8859_1"), "utf-8");
    // txtContent=new String(txtContent.getBytes("8859_1"));//乱码
    // 提取数据
    int pos1 = txtContent.indexOf("<title");
    int pos2 = txtContent.indexOf("</title>");
    txtContent = txtContent.substring(pos1, pos2);
    // 验证是否一致

      if(txtContent.equals("<title>优尼->提示页面")==true){
      System.out.println("一样"); }else{ System.out.println("不一样"); }
      System.out.println(txtContent);// 输出要判断的数据
    /*if (txtContent == "<title>优尼->提示页面") {
    System.out.println("一样");
    } else {
    System.out.println("不一样");
    }*/ } catch (IOException io2) {
    System.out.println("IOException异常: " + io2.toString());
    } } catch (MalformedURLException e) {
    System.out.println("MalformedURLException异常:" + e.toString());
    } }}
    把我这段代码放进去执行,输出:一样
    <title>优尼->提示页面
      

  8.   

    if ("<title>优尼->提示页面".equals(txtContent) )
    {
    System.out.println("一样");
    }
    else
    {
    System.out.println("不一样");
    }
    我就改了红色部分的一句话,结果输出:
    <title>优尼->提示页面
    一样问题出在你的project的编码,因为你的类中用到的字符串本身是有编码的,我的project里用到的编码是"UTF-8"。
      

  9.   

     字符串之间的比较用equals啊。  用==是比较他们的内存地址。 你去网上找找equals和==的区别吧。 
     
      

  10.   

    if(txtContent.equals("<title>优尼->提示页面")==true){
    equals关键字