日期:2014-05-20  浏览次数:20994 次

请教一个 Java String 的问题..
大意是判断一个字符串,通过split分隔后,是否包含"?"子串, 具体见下面红色部分..

Java code

    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(",");          [color=#FF0000]//将文件adult.txt中的每一行以逗号分隔开, 返回字符串数组temp,[/color]
                String[] temp1 = new String[8];     [color=#FF0000] //用于取temp字符串数组中特定的字段..[/color]
                
                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("?"))       [color=#FF0000]//判断所取字段中是否有"?"这样的字符串,原adult数据中有的行是有?号存在的,为什么这里检测不出来?[/color]
                        {
                          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) {

        }

    }



------解决方案--------------------
应该用 temp1[i].contains("?") 判断 temp1[i]中是否含有 ?
你之前的判断是判断 temp1[i] 字符串是否是 ?