3

I have a data in my String object which is like 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 found words (Inlist ,NotEqual,Inlist ) then it need to break the line and need toread the next line.

Output would be:

Scanner 
Resolved scan set
Area of intrest
Initial responder

Till now 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);
         }
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
Navyah
  • 1,660
  • 10
  • 33
  • 58
  • It is storing the string data 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 – Navyah Feb 01 '13 at 12:36

5 Answers5

3

You have a big flaw in your logic:

Looking at your if I see

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

How can Inlistremove ever be equal to "InList" AND be equal to "NotEqual" at the same time? Are you looking for OR? That would be ||

jlordo
  • 37,490
  • 6
  • 58
  • 83
1

Use this line:

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

instead of

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

EDIT
Ok then watch 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());
    }

}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
1

Very flexible, only 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"));
}

Will print:

Resolved scan set

Area of intrest

Total Intrest Initial responder

Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29
0

You don't need to use StringTokenizer. Plese see an old question to know why?

Instead of StringTokenizer you can use regex to match the parts of string which you don't need and replace them with empty string.

Community
  • 1
  • 1
  • -1 You should put this as a comment, it doesn't answer the question – fmsf Feb 01 '13 at 12:32
  • really bad thinking, its not replacing, its about ignoring. – Aniket Inge Feb 01 '13 at 12:33
  • I cant yet add comments. I can add comments to my own answers only. – Piyush Joshi Feb 01 '13 at 12:33
  • @fmsf 2nd paragraph does answer the question, which is about removing end of line after it matches a keyword, before printing it... Though it could of course provide regexp to do it. Also first paragraph raises a very valid point about `StringTokenizer`, with supporting link. – hyde Feb 01 '13 at 12:41
0

Three problems in your code:

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

which should be

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

Second is here:

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

which should be:

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

The third one is, how will you move to the next line of the string array? You have to change the code slightly to incorporate moving to another string to parse.

For that, I suggest you to 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 main() method you do this:

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]);
    }
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78