Results 1 to 11 of 11
- 04-27-2011, 04:37 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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
- 04-27-2011, 04:41 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Removed post
Last edited by sunde887; 04-27-2011 at 04:59 AM.
- 04-27-2011, 04:42 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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; }
- 04-27-2011, 04:46 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
... (too slow)
- 04-27-2011, 04:49 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-27-2011, 04:51 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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; } }
- 04-27-2011, 05:03 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 04-27-2011, 05:12 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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.
- 04-27-2011, 05:16 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-27-2011, 05:19 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
got it. Thanks! I will test this out.
- 04-27-2011, 05:36 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
-
array containing int strings
By ggalan in forum New To JavaReplies: 19Last Post: 04-22-2011, 04:40 PM -
Checking ascending order of array
By counterfox in forum New To JavaReplies: 3Last Post: 10-22-2010, 10:44 PM -
Checking if something is equals to anything in an array
By Sapster in forum New To JavaReplies: 5Last Post: 03-19-2010, 12:26 AM -
Strings as array help please..
By crazygurl in forum New To JavaReplies: 1Last Post: 12-01-2009, 08:23 PM -
Need help with counting strings in an array.
By dalonehunter in forum New To JavaReplies: 1Last Post: 10-02-2009, 08:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks