Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
John Jai wrote:Also you can use regex - Pattern and Matcher.
Winston Gutkowski wrote:
John Jai wrote:Also you can use regex - Pattern and Matcher.
Or indeed indexOf(String).
@Dan Ra: I think one of the things you need to be clear about is what you mean by "word matching".
For example, if
userStr="The quick brown fox jumps over the lazy dog"
and
userSearch="jump"
do you want the result to be 1 or 0?
Winston
Dan Ra wrote:if the userSearch completely matches a word in userStr, i want to count it as 1.
for ex: userStr = "I have a coat. My coat is brown."
and
userSearch = "coat"
I want the output to be: "The word "coat" was found 2 times."
Winston Gutkowski wrote:
Yes, but that doesn't fully answer the question, since a simple string match would produce the same result. What do you want if
userStr = "I have two coats. My coat is brown."
and
userSearch = "coat"
1 or 2?
Winston
Dan Ra wrote:oh sorry about that. i would like the result to be 1.
Panagiotis Kalogeropoulos wrote:Your code is:
The signature of the substring method is substring(int beginIndex,int endIndex)
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#substring(int, int)
In your code, you are passing as arguments a char and an int. This is where the exception occurs. But can you guess why this happens? (hint: what is the relationship between an int and a char?)
No, it isn’t. Just because they are all types of integer does not mean a char is an int.Panagiotis Kalogeropoulos wrote: . . . a char is in a sense an int (just like an int is a long and a long is a double). . . .
Dan Ra wrote:is there a way i can do this with substring? i'm trying to stay away from regexes.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here