Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Comparing 2 Strings with 1?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm trying to create this following Program:

Return true if the string "cat" and "dog" appear the same number of times in the given string.

catDog("catdog") → true
catDog("catcat") → false
catDog("1cat1cadodog") → true


..............................................................................................................................................

This is what I have been trying to do but it wont work!


 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Make sure you use the equals() method to compare strings. The "==" is used to compare references, and it is doubtful that the value returned from substring will be the same object as the one created for the string literal.

Henry
 
Sheriff
Posts: 9679
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can, use regular expression to achieve this...
 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Make sure you use the equals() method to compare strings. The "==" is used to compare references, and it is doubtful that the value returned from substring will be the same object as the one created for the string literal.

Henry



Why do you think that its doubtful that the value returned from substring will be the same object as the one created for the string literal???
 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khoder Wassouf wrote:

Henry Wong wrote:

Make sure you use the equals() method to compare strings. The "==" is used to compare references, and it is doubtful that the value returned from substring will be the same object as the one created for the string literal.

Henry



Why do you think that its doubtful that the value returned from substring will be the same object as the one created for the string literal???



I did what you said but still same error!

 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khoder Wassouf wrote:
I did what you said but still same error!



You do know that you never actually told us what this error that you encountered is, right?

Henry
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:If you can, use regular expression to achieve this...



Agreed. Implemented it in five minutes...

Henry
 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Ankit Garg wrote:If you can, use regular expression to achieve this...



Agreed. Implemented it in five minutes...

Henry



It doesnt tell me what the error is..............it just compiles and then tells me there's an error on line......

But since you got it working with Regular expression....are you talking about the .matches one??
If you can also give me a copy of the code that worked with you
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khoder Wassouf wrote:
It doesnt tell me what the error is..............it just compiles and then tells me there's an error on line......



It does tell you what the error is. You just need to know how to read it.

Anyway, just compiled and ran your program. It is an array out of bounds error -- basically, the array that you instantiated is not big enough for how you are using it. This is actually not surprising, since it looks like the array that you are creating are of size zero.

Henry
 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Khoder Wassouf wrote:
It doesnt tell me what the error is..............it just compiles and then tells me there's an error on line......



It does tell you what the error is. You just need to know how to read it.

Anyway, just compiled and ran your program. It is an array out of bounds error -- basically, the array that you instantiated is not big enough for how you are using it. This is actually not surprising, since it looks like the array that you are creating are of size zero.

Henry



Hey Henry.........Mate I have only been programming in Java for 4 days now!.....getting ready for the SCJP exam which is in Mid January.
I have been programming in C# for 4 years but its been like a Year since I touched it

But anyway........This is what I got:




Help! .................Thanks
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically, you created an array of size zero at line 23. The for-loop starting at line 26 does absolutely nothing -- because the condition never succeeds. And then it dies at line 50, because you try to access the first element of the zero element array that you created at line 23.

Henry
 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Basically, you created an array of size zero at line 23. The for-loop starting at line 26 does absolutely nothing -- because the condition never succeeds. And then it dies at line 50, because you try to access the first element of the zero element array that you created at line 23.

Henry



Ok Henry thanks.....it all makes sense now

So what Procedure would you advise me to use in order to compare the String's??..........I understand that you advised me to use the Regular expression.....Did that mean the .matches()???..............or what??

Thanks!
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khoder Wassouf wrote:
just tell me where I should start looking at and what I should start trying!



I would recommend that you examine your code, and figure out why it is not following your design. In this case, why the condition is always failing. After all, you wrote all that code, why are you so easily willing to just dump it?

Now... not to be condescending -- it is just that newbies love to skip the design stage and jump immediately into code.... If you haven't figured out the algorithm yet, then I recommend that you stop; Take out a pen and paper, and figure out how to do it on paper first. You need to be able to do this yourself, on paper, before you are able to teach the computer how to do it. Just pretend you are the computer and work it out in very little steps. Once you do that, then you can convert the little steps into lines of code.

Henry

 
Khoder Wassouf
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean Henry and what your saying Its very true....... problem is Iv got the exam in mid January......So I was in a Hurry :P

Anyway thanks...... I re did the program and posted a new topic of a more meaningful question with a better code for this program!

Thanks again

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi....


S1.length() - str.length()

I had Seen the above condition of for loop in check method..
But i m nt able to get it...
As you passing str String of length 6 char and S1 is of 3 it will give-3
but the condition look like false at first time....
Please elabrte about for loop condition
 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

I agree and appreciate the thought you guys have given while creating the rationales in JavaRanch forum.


Thanks,
-Hrishikesh
 
reply
    Bookmark Topic Watch Topic
  • New Topic