明明应该一样的数据,却判断为不一样,实在想不通,怎样能判断成一样吗
代码如下:
/*********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());
} }
}
代码如下:
/*********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());
} }
}
解决方案 »
- 一个java问题
- JAVA程序设计视频教程
- 如何将JCombobox 内容排序?
- log4j的debug信息太多了,想去掉一点没用的。
- 请问,我要用java编写一个数据转化软件,思路该如何走,数据和转换后的要求如下
- 数组问题,高手请进
- 请问new ActionError第二个参数的功能是什么:errors.add("username",new ActionError("username_wrong","ghyghost"));
- 很郁闷啊java竟然找不到把jpg 转 成dicom格式的方法
- 急需帮忙
- java中long型数据存入MSsql地问题
- javamail问题,急等
- 为什么会没有输出???
不过在调用equals方法前必须验证调用equals的方法不能为null
:
--------------------配置: <默认>--------------------
D:\Test\InputStreamExample.java:40: 找不到符号
符号: 方法 Equals(java.lang.String)
位置: 类 java.lang.String
if(txtContent.Equals("<title>优尼->提示页面")==true){
^
1 错误处理已完成。
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>优尼->提示页面
{
System.out.println("一样");
}
else
{
System.out.println("不一样");
}
我就改了红色部分的一句话,结果输出:
<title>优尼->提示页面
一样问题出在你的project的编码,因为你的类中用到的字符串本身是有编码的,我的project里用到的编码是"UTF-8"。
equals关键字