Results 1 to 11 of 11
  1. #1
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default Checking for two strings in array at the same time

    Here is my problem. I have a String array:

    String[] array = {"Entry engineer name is Alex",
    "Junior engineer name is Bob",
    "Senior engineer name is Jacob",
    "Principal engineer name is Steve"}

    I have a method that checks if two strings are present together in one of the array elements. Basically check that "Junior" and "Bob" is present in one element "Junior engineer name is Bob".

    Basically this is the calls I need to check:

    isTextPresentInArray(array, "Junior", "Bob"); //should return true
    isTextPresentInArray(array, "Bob"); // should return true
    isTextPresentInArray(array, "Entry", "Bob"); //should return false


    This is as far as I got in the implementation:

    boolean isTextPresentInArray(String[] array, String... text) {
    boolean result = false;
    for(String entry : array) {
    for (int i = 0; i < text.length; i++) {
    result = entry.contains(text[i]);
    } // end of inner loop
    if(result) {
    return true;
    } // end of outer loop
    return false;
    }

    This is as far as I got and I think all the calls should work except last one where it will return true whereas it should return false. How do I fix this to work?

    thanks!
    Veronique

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Removed post
    Last edited by sunde887; 04-27-2011 at 05:59 AM.

  3. #3
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    not sure how to format it here for easier reading

    Java Code:
    boolean isTextPresentInArray(String[] array, String... text) {
      boolean result = false;
      for(String entry : array) {
        for (int i = 0; i < text.length; i++) {
          result = entry.contains(text[i]);
        } // end of inner loop
        if(result) {
          return true;
        } // end of outer loop
      return false;
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    ... (too slow)

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I re read your code, disregard what I said in the previous post. I'll look over it some more and see if I can see what's wrong.

  6. #6
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    adding missing bracket:

    Java Code:
    boolean isTextPresentInArray(String[] array, String... text) {
      boolean result = false;
      for(String entry : array) {
        for (int i = 0; i < text.length; i++) {
          result = entry.contains(text[i]);
        } // end of inner loop
        if(result) {
          return true;
        } // end of outer loop
      return false;
     }
    }

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your error is because it checks the second element in the array, it doesn't contain entry so it sets the flag to false, then it checks the second element for bob, which it does contain, it sets the flag to true, then it performs the condition test outside the inner loop and returns true. If the first element doesn't exist, should you check the second?

  8. #8
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Ah I see where you are going with that. So we just add if(!result) return false; after result = entry.contains(text[i]); call to fix this?

    Probably no need to check for second element when first is not present.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sort of, you don't want to return false until you have checked every element. If the first element isn't found it may be better to break out of the loop.

  10. #10
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    got it. Thanks! I will test this out.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, if you are done, please mark your thread solved with the thread tools at the top.

    If you are not done, feel free to ask more questions.

Similar Threads

  1. array containing int strings
    By ggalan in forum New To Java
    Replies: 19
    Last Post: 04-22-2011, 05:40 PM
  2. Checking ascending order of array
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 11:44 PM
  3. Replies: 5
    Last Post: 03-19-2010, 01:26 AM
  4. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 09:23 PM
  5. Need help with counting strings in an array.
    By dalonehunter in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 09:47 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
  •