3

我的 String 对象中有一个数据,如下所示

Scanner Inlist 1,2,3
 Resolved scan set NotEqual to  Non Scan Set
 Area of intrest equal to Total Intrest
 Initial responder Inlist enter values

现在,当我阅读每一行时,如果我找到了单词 (Inlist ,NotEqual,Inlist ),那么它需要换行并需要阅读下一行。

Output would be:

Scanner 
Resolved scan set
Area of intrest
Initial responder

到目前为止,我尝试过的是

String filterstringobj=promtchild.toString();
StringTokenizer str=new StringTokenizer(filterstringobj,"");
while(str.hasMoreTokens())
{
    String Inlistremove=str.nextToken("InList");
    if(Inlistremove.length()!=0)
    {                       
         System.out.println(Inlistremove);
         if(Inlistremove.equalsIgnoreCase("InList") && 
            Inlistremove.equalsIgnoreCase("NotEqual") && 
            Inlistremove.equalsIgnoreCase("Equal")
           )
         {
            System.out.println(Inlistremove);
         }
    }
}
4

5 回答 5

3

你的逻辑有一个很大的缺陷:

看着你的if我看到了

if(Inlistremove.equalsIgnoreCase("InList")&&Inlistremove.equalsIgnoreCase("NotEqual")&&...

怎么可能Inlistremove同时等于"InList" AND等于"NotEqual"?您在寻找吗?那将是||

于 2013-02-01T12:32:04.763 回答
1

使用这一行:

StringTokenizer str=new StringTokenizer(filterstringobj," "); 

代替

StringTokenizer str=new StringTokenizer(filterstringobj,"");

编辑
好的,然后观看以下演示代码:

import java.util.StringTokenizer;
class  WordsFromString
{
    public static void main(String st[])
    {
        String data = "Scanner Inlist 1,2,3\n"+
                      "Resolved scan set NotEqual to  Non Scan Set\n"+
                      "Area of intrest equal to Total Intrest\n"+
                      "Initial responder Inlist enter values";
        StringTokenizer tokenizer = new StringTokenizer(data,"\n",true);
        StringBuilder output = new StringBuilder();
        while (tokenizer.hasMoreElements())
        {
            String sLine = tokenizer.nextToken();
            StringTokenizer tokenizerWord = new StringTokenizer(sLine," ",true);
            while (tokenizerWord.hasMoreElements())
            {
                String word = tokenizerWord.nextToken();
                if ("Inlist".equals(word) || "NotEqual".equals(word) || "Inlist".equals(word) || "equal".equals(word))
                {
                    break;
                }
                else
                {
                    output.append(word);
                }
            }
        }
        System.out.println(output.toString());
    }

}
于 2013-02-01T12:29:39.940 回答
1

非常灵活,只有一行:

public static String parseLine(String line){
    return line.replaceAll("(?i)(inlist|notequal|equal).*", "");
}

public static void main(String[] a){
    System.out.println(parseLine("Resolved scan set NotEqual to  Non Scan Set"));
    System.out.println(parseLine("Area of intrest equal to Total Intrest"));
    System.out.println(parseLine("Initial responder Inlist enter values"));
}

将打印:

解析扫描集

感兴趣的领域

总利息 初始响应者

于 2013-02-01T12:36:40.847 回答
0

您不需要使用 StringTokenizer。请看一个老问题知道为什么?

您可以使用正则表达式代替 StringTokenizer 来匹配您不需要的字符串部分并将它们替换为空字符串。

于 2013-02-01T12:32:22.843 回答
0

您的代码中存在三个问题:

StringTokenizer str=new StringTokenizer(filterstringobj,""); 

应该是

StringTokenizer str=new StringTokenizer(filterstringobj," "); 

第二个在这里:

if(Inlistremove.equalsIgnoreCase("InList") && 
            Inlistremove.equalsIgnoreCase("NotEqual") && 
            Inlistremove.equalsIgnoreCase("Equal")
           )

应该是:

if(Inlistremove.equalsIgnoreCase("InList") ||
            Inlistremove.equalsIgnoreCase("NotEqual") || 
            Inlistremove.equalsIgnoreCase("Equal")
           )

第三个是,你将如何移动到字符串数组的下一行?您必须稍微更改代码以将移动到另一个字符串进行解析。

为此,我建议您创建一个函数:

public static void Parse(String s){ 
   String filterstringobj=s;
   StringTokenizer str=new StringTokenizer(filterstringobj," ");
   while(str.hasMoreTokens())
   {
       String Inlistremove=str.nextToken("InList");
       if(Inlistremove.length()!=0)
       {                       
           System.out.println(Inlistremove);
            if(Inlistremove.equalsIgnoreCase("InList") ||
               Inlistremove.equalsIgnoreCase("NotEqual") ||
               Inlistremove.equalsIgnoreCase("Equal")
            )
           {
               System.out.println(Inlistremove);
               return;
           }
      }
   }
}

main()你这样做的方法中:

public static void main(String[] args)
{
    String[] array = new String[3];
    array[0] = "Resolved scan set NotEqual to  Non Scan Set";
    array[1] = "Area of intrest equal to Total Intrest";
    array[2] = "Initial responder Inlist enter values";
    for(int i = 0; i < 3; i++) {
          Parse(array[i]);
    }
}
于 2013-02-01T12:35:21.697 回答