Results 1 to 3 of 3
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Question how to find more than one String with single command

    hi,
    here is my program that can read through a txt file line by line to find one word named "friends". if any line contains of this word then get the line and check my precised friend's name is mentioned there or not ?

    its working when my precised named only one, but what should i do if it will more than one ?

    suppose, my txt file contains like this:
    my childhood friends named Jassei and Kathy.
    Abbey, Kathy and Borny were my school friends.
    In college, Jami and Abbey were good friends.
    Here in university, Jacky and Hatchi are my new friends.
    and code:
    Java Code:
    String target="friends";
    String reading=new String();
    String findName="Kathy";
    
    try{
    FileReader fr = new FileReader ("C:/Java/Test1.txt");   
    BufferedReader reader = new BufferedReader (fr);
    
    while (reader.ready()){ 
    reading=reader.readLine();
                                    
    if (reading.indexOf(target)!= -1 ){ 
    String line = result.substring(0, result.indexOf(target));
                                            
    if (line.indexOf(findName) !=-1) {
    System.out.println("Name found");
    }
    }
    }
    }
    catch(IOException ioe){}
    my question is, at a time i want to search more friend names not only "kathy"..

    if i use like this,
    String[] findNames={"kathy", "Borny", "Jakcy"}
    then what to do ??
    or any idea pls ??
    what we are thinking, it might not be true

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by doha786 View Post
    my question is, at a time i want to search more friend names not only "kathy"..

    if i use like this,
    String[] findNames={"kathy", "Borny", "Jakcy"}
    then what to do ??
    or any idea pls ??
    Loop through the array with a for-loop.

    Also, if you indent your pasted code so that it's easier to read it will be easier for us to help you. Thanks in advance.

    Oh, and you'll really want to avoid empty catches:
    Java Code:
    catch(IOException ioe){}
    because how will you know if an error occurs, why it has occurred?

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    From what you are describing, it seems you want to implement this:
    Java Code:
        public boolean ifAnyNameFound(String target, String[] namesToFind) {
            ...
        }
    -Gary-

Similar Threads

  1. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  2. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  3. Find a number from a string
    By florentp in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 09:01 PM
  4. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 11:51 AM
  5. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM

Posting Permissions

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