How to break a string when a word was found

I have data in my String object which is below

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

      

Now when I read each line, if I find words (Inlist, NotEqual, Inlist) then it needs to break the line and require the next line.

Output would be:

Scanner 
Resolved scan set
Area of intrest
Initial responder

      

So far, what I have tried is

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);
         }
    }
}

      

+3


source to share


5 answers


Use this line:

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

      

instead



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

      

EDIT
So check out the following demo code:

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());
    }

}

      

+1


source


You have a big flaw in your logic:

Looking at yours if

I can see



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

      

How can one Inlistremove

ever be equal "InList"

AND equal "NotEqual"

at the same time? Are you looking for OR ? It will be||

+3


source


Very flexible, just one line:

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"));
}

      

This will print:

Set of allowed scans

Interior area

Final initial responder

+1


source


You don't need to use StringTokenizer. Plese see the old question to find out why?

Instead of a StringTokenizer, you can use a regular expression to match parts of a string you don't need and replace them with an empty string.

0


source


Three problems in the code:

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

      

which should be

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

      

Second:

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

      

which should be:

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

      

Third, how do you go to the next line of the string array? You have to modify the code slightly to include moving to another line for parsing.

For this, I suggest you create a function:

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;
           }
      }
   }
}

      

and in the method main()

you do the following:

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]);
    }
}

      

0


source







All Articles