Results 1 to 9 of 9
  1. #1
    gazale_m's Avatar
    gazale_m is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default searchin for more Strings in a Text

    Please, I need your HELP!! In my Program, I open a file, read it line by line and find the word „Page“in it.
    Now I will that my Program find more than 1 word. :confused:
    Java Code:
    .
    .
    String line = dis.readLine();
    int linem =1;
    while (line != null) {
                 String searchText = "Page";
                if(line.indexOf(searchText) !=-1) // or if(line.contains(searchText))
                       {
                      underline(line,searchText);
                        }
               linem++;
             line = dis.readLine();
    }
    .
    .
    Thanks for your Help!

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    You mean you want to find multiple occurances of Page; or you want to find different words(Page, Chapter) at the same time?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You want to find only one? Can you explain it more clearly. If a line of the file as follows what's your expectation as the result.

    This page has more page words.

  4. #4
    gazale_m's Avatar
    gazale_m is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    yes I need to find more words. That means in your (Eranga) sentence :
    This page has more page words.
    I want to find : page and word (for example)

    Note !!! ::eek:
    1. my program underlines the words.
    2. I dont want to give out how many times the file contains the word.

    thank you for your Help!

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default loop and array

    • Populate an array with the words you want to look for
    • Create a "for" loop that takes each word from the array and compares against the sentence (like you do currently)
    • Maybe something like:

    Java Code:
    For (i=0; i < myArray.length; i++)
        {
         Underline stuff;
        }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    gazale_m's Avatar
    gazale_m is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks, it remind me of a good solution :

    String searchText = "Page";
    String searchText1 = "State";
    if((line.indexOf(searchText) !=-1) || (line.indexOf(searchText1) !=-1))
    {
    underline(line,searchText);
    underline(line,searchText1);
    }

    its not a nice way but it does the work. I prefere to use Regular Expression but still don't know hoe the method works !!:cool:

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default That's not a good solution

    That is not a good solution. It's functional, but not good. The way it is coded, only one of the conditions of the "if" statement has to be true for it to enter the code in the "if" and I don't know if the underline method can handle a condition where the word is NOT in the sentence.
    Again, a more robust solution would be to use an array and a loop (ex: for loop). These are not complex commands and very useful.
    The "for" loop would look something like:
    Java Code:
    For (i=0; i < myArray.length; i++)
        {
         if (line.indexOf(myArray[i] != -1)
          {
           underline(line,myArray[i]);
          }
        }
    Some links:
    Arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    for loops:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Java SE API 6:
    Java Platform SE 6
    Sun Java tutorial:
    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gazale_m View Post
    yes I need to find more words. That means in your (Eranga) sentence :

    I want to find : page and word (for example)

    Note !!! ::eek:
    1. my program underlines the words.
    2. I dont want to give out how many times the file contains the word.

    thank you for your Help!
    I'm bit confusing on this lol. Do you want to underline all words or the occurrence of two words next to each other?

  9. #9
    gazale_m's Avatar
    gazale_m is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    to CJSLMAN :
    thanks for the links but I think instead of Arrays I could use List<String>! do you know a link about it too? or could you give me an example?
    Java Code:
    List<String> woerter = new ArrayList<String>();
    how can I use it in my Prog.?
    Java Code:
    String line = dis.readLine();
    			      int linem =1;
    			      while (line != null) {
    			    	  
    			            System.out.println(line);
    			            String searchText ="Page" ;
    			            String searchText1 ="State" ;
    			            
    			           if((line.contains(searchText))||(line.contains(searchText1))){
    			           //if(line.indexOf(searchText) !=-1){
    			           	   underline(line,searchText);
    			        	   underline(line,searchText1);
    			            }
    			      linem++;            
    		          line = dis.readLine();
    to Eranga:
    I kann find the words now and underline them too. But what I want is to find more Words !!

Similar Threads

  1. printing simple text as text on printer
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2008, 02:42 AM
  2. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  3. Replies: 5
    Last Post: 07-18-2008, 05:30 PM
  4. Replies: 0
    Last Post: 05-26-2008, 05:26 PM
  5. text Strings to produce letters
    By dc2acgsr99 in forum New To Java
    Replies: 7
    Last Post: 01-29-2008, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •