请教一个 Java String 的问题..

realdragon2 2009-01-11 07:06:09
大意是判断一个字符串,通过split分隔后,是否包含"?"子串, 具体见下面红色部分..


public void Handle() {
try {
FileReader fs = new FileReader("adult.txt");
BufferedReader br = new BufferedReader(fs);

FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);

String strLine = null;
String[] temp;
strLine = br.readLine();
int m = 0;

boolean test=false;
String strTemp = null;

while (strLine != null) {
temp = strLine.split(","); //将文件adult.txt中的每一行以逗号分隔开, 返回字符串数组temp,
String[] temp1 = new String[8]; //用于取temp字符串数组中特定的字段..

temp1[0] =temp[0].substring(0, temp[0].length());
temp1[1] =temp[1].substring(0, temp[1].length());
temp1[2] =temp[3].substring(0, temp[3].length());
temp1[3] =temp[5].substring(0, temp[5].length());
temp1[4] =temp[6].substring(0, temp[6].length());
temp1[5] =temp[8].substring(0, temp[8].length());
temp1[6] =temp[9].substring(0, temp[9].length());
temp1[7] =temp[13].substring(0, temp[13].length());

for(int i=0;i<8;i++)
if(temp1[i].equals("?")) //判断所取字段中是否有"?"这样的字符串,原adult数据中有的行是有?号存在的,为什么这里检测不出来?
{
test=true;
break;
}
if(test!=true)
{
strTemp = temp1[0] + " " + temp1[1] + " " + temp1[2] + " "
+ temp1[3] + " " + temp1[4] + " " + temp1[5] + " "
+ temp1[6] + " " + temp1[7] + " " +"\n";
for (int i = 0; i < 8; i++) {
System.out.print(temp1[i] + " ");
}
System.out.println();

m++;
bw.write(strTemp);
strLine = br.readLine();
}
else
{
strLine = br.readLine();
continue;
}
}
System.out.println("i= " + m + " ");
bw.close();
fw.close();
br.close();
fs.close();

} catch (IOException io) {

}

}
...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
realdragon2 2009-01-11
  • 打赏
  • 举报
回复
搞定, so简单的问题,呵呵. thanks.
realdragon2 2009-01-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bzwm 的回复:]
应该用 temp1[i].contains("?") 判断 temp1[i]中是否含有 ?
String实例中有这个函数没? 土了.. 我查一下..呵呵
你之前的判断是判断 temp1[i] 字符串是否是 ?
是的, temp1[i]是否等于?
[/Quote]
bzwm 2009-01-11
  • 打赏
  • 举报
回复
应该用 temp1[i].contains("?") 判断 temp1[i]中是否含有 ?
你之前的判断是判断 temp1[i] 字符串是否是 ?
bzwm 2009-01-11
  • 打赏
  • 举报
回复
应该用 temp1[i].contains("?") 判断 temp1[i]中是否含有 ?
你之前的判断是判断 temp1[i] 字符串是否是 ?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧