This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Contains and not contains use  RSS feed

 
Ranch Hand
Posts: 41
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this program fragment that confuses me a bit:


To my understanding the contains syntax is as follows:
stringx.contains("valuetobesearchedfor") - checks if valuetobesearchedfor is present in stringx and returns true if it is
I have not actually seen an example of how to use the opposite of contains to check if something is NOT contained in string, but I imagine it would work like:
!stringx.contains("valuetobesearchedfor") - then it would return true if the valuetobesearchedfor is NOT present in there

So what is up with the syntax in my fragment? (It's from exam papers, preparing for exam in 2 days... not going great lol)
Why is the value to be searched for placed before the string to search in? Why are the places reversed? Makes it seem like you're checking to see if "aeiou" has the value of c in it rather than the other way around.
So is this line asking if any of the characters "aeiou" are NOT contained in string c then you print string c or if any of the string "aeiou" is contained in c then you print?

Also the result is confusing. Apparently the output should be c. But during each run of the for loop you add elements from string x to substring c and then only print string c when condition is met. But in the end when condition is met, you still have to print the entire string c, which should be "ace" but that's not even an option on the paper. I'm so confused.
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suane Mane wrote:Why is the value to be searched for placed before the string to search in? Why are the places reversed? Makes it seem like you're checking to see if "aeiou" has the value of c in it rather than the other way around.
So is this line asking if any of the characters "aeiou" are NOT contained in string c then you print string c or if any of the string "aeiou" is contained in c then you print?

Also the result is confusing. Apparently the output should be c. But during each run of the for loop you add elements from string x to substring c and then only print string c when condition is met. But in the end when condition is met, you still have to print the entire string c, which should be "ace" but that's not even an option on the paper. I'm so confused.



Yes, I can see how that's confusing. But the contains() method simply asks whether the string on the left contains the string on the right. Whether either of those two strings is a variable or a string literal makes no difference.

You naturally assume that you're going to ask whether some unknown string contains a string which you already know about. In this case you'd write if (unknown.contains("banana")), for example. But the example in your code is asking whether a string you already know about contains some unknown string. In particular it is asking whether the string "aeiou" contains whatever string the variable c represents.

For example "aeiou".contains("io") is true, but "aeiou".contains("eo") is false.

And if you want to know if some string value does NOT contain some other string value, then you just put the not-operator ! before the call to the contains method, as in the code fragment.

For example !"aeiou".contains("io") is false, but !"aeiou".contains("eo") is true.

To understand the whole question you have to look at the first three lines of code and figure out what values the variable c has while the fragment is running, and then apply that business about the contains method to those values.

Hope that helps, if not then post back with more questions.
 
Suane Mane
Ranch Hand
Posts: 41
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it now! So it's the same thing, just using a known string.
One thing is still confusing though - line 3: String c = x.substring(i, i + 1);
Doesn't this mean you are adding elements to string c from string x? Whether or not you print them, they are added. So this loop runs 3 times.
->First adding "a" to c, not printing
->then adding "c" to c, printing string c
->then adding "e" to c, not printing.

At the time of print, substring c has values "ac". Why then does print output only "c"?
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across Sets yet? You can create a Set containing the letters from “aeiou” and another Set for ”au” and see whether the two are subsets of each other.
 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suane Mane wrote:
At the time of print, substring c has values "ac". Why then does print output only "c"?



There's actually a number of reasons to this.

The basic one is that c is not an object, it's a reference.  It points to an object.
So c = <something> is telling c (assuming it is a reference and not a primitve like an int) to point to a different object.

So, in your example, c is initially pointing to a String object with the value "a".  Then it is pointing to a String object with the value "c", and so on.

Now, the above would apply if the code were:


But with your code, c is declared inisde the loop, so it is actually a new variable each time (essentially), so it's not being reassigned.

Either way, it does not append the value (there's methods for that).  It changes it.

Hopefully the above makes some sense...I'm not convinced I've written that as clearly as I intended!
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:I'm not convinced I've written that as clearly as I intended!


I can assure you, not worse than I would have been.
 
Liutauras Vilda
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If such an exercise is for the cs's course exam, during the stressful time it can get annoying very quickly tracking information floating around if you don't write values on a piece of paper..
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!