• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help counting phrases in a string  RSS feed

 
Dan Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm supposed to prompt the user for a string, then prompt them for a second string which would then search the first for the amount of times it shows up. i would then say that the word that they searched for was found x amount of times in the first string they entered.
i can only use loops such as for and do while, and if-else statements.
this is what i have so far:


output is supposed to be: "The word "x" was found "y" times."

please help.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan Ra,

Welcome to CodeRanch!

What you are doing is - increasing the counter for each matching character . So, if second String is say 'yes', counter will be 3 when 'yes' is found in first String.

You can do following:

1) Split the first String with spaces (' ' character)
2) If you want the exact words and not words containing that word, then you call equals method on each of those Strings
3) Otherwise, call contains on them

I hope this helps.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you can use regex - Pattern and Matcher. Check for a sample code here.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


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."

sorry if i was being a little unclear and thanks for the advice so far
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."

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 Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


oh sorry about that. i would like the result to be 1.
another example: userStr = "I have five apples. I gave away one apple."
and
userSearch = "apple"
the output would be: "The word "apple" was found 1 time."
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Ra wrote:oh sorry about that. i would like the result to be 1.

OK, so you are doing a full-word match. It should be noted that that has its own 'gotchas', just a few of which are:
1. You have to define what a "word" means in your string.
2. You may have to include a previous "word boundary" character in a subsequent search.
3. you should handle repeated "word boundary" characters correctly (eg, "I have    five apples.     I gave away   one.".
and I've probably missed a few others.

Regexes handle all this stuff very nicely; but if this is an exercise, then you have to do it yourself.

Winston
 
Dan Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way i can do this with substring? i'm trying to stay away from regexes. i have this so far but i get an out of bounds exception:

 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Dan Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)


my substring is a char and and an int instead of both of them being ints?
substring(char, int) instead of substring (int,int)?
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no. The substring method requires (int,int) and you provide (char,int). The reason you are allowed to do this is because a char is in a sense an int (just like an int is a long and a long is a double). This has to do with how primitives work in Java. In order to stay on topic (and to answer your question) you should make sure that you pass as arguments to the substring (int,int) and not (char,int). But after that, it would be good if you learned how primitives work (which will make it easier for you to understand how other things work, like autoboxing in generics). After you're done with the program that you posted here, maybe you could take a look at the code below and try to figure out how this works (this will also help you understand why you have an out of bounds exception):



Keep us posted if you find any difficulty.
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will also help you about primitives:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Cheers!
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panagiotis Kalogeropoulos wrote: . . . a char is in a sense an int (just like an int is a long and a long is a double). . . .
No, it isn’t. Just because they are all types of integer does not mean a char is an int.

But you have a good point. Many people think that chars hold characters, which is quite incorrect. They are unsigned integers. Obviously using 'a' as an argument for a method like substring which expects an int will result in the 'a' undergoing a widening conversion to (int)97.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Ra wrote:is there a way i can do this with substring? i'm trying to stay away from regexes.

I think I'd probably use indexOf() to find the string initially (look it up in the java.lang.String API), and then something like charAt() on the characters before and after the string I've found to work out whether it's a "word" or not; but, as I said before, you need to be quite clear about what a "word" actually means.

Winston
 
Dan Rana
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it to work sort of and this is how i did it:



it does count the word "apple" as one even if the str is "apples" , however. thanks for all the help guys
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have used the String class's contains method as well. Just my two cents.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!