• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Analyze problem  RSS feed

 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working through tutorials on here, in the heads first java book and programmr.com but for some reason I can not find the answer to this.

Basically the requirements are to take a sentence (string of text) and check to see how many times specific words come up and then add to the counter depending on the word.
But I can not seem to get it to add the instances of the goodwords and badwords. Any suggestions?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your analyse method looks like it was coded as you were deciding how to solve the problem rather than working out how to solve the problem using pencil and paper and then (and only then) turning on your computer and turning your solution into code.
I suggest you turn off your computer and write down in English (or your native language) how to do it and imagine your notes are going to be used by a 7 year old so make them really simple and easy to understand.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the issue that you are facing?
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:What is the issue that you are facing?


(I have updated the code above)


1) I get the following:
2) Enter the text with spaces(press enter to done):
3) Good and Bad
4) The phrase is: Good and Bad
5) Phrase is 3 words long.
6) 0 0
7) Line:
8) 0P0N


It should come back with 1 1 on line 6 and 1P1N on line 8. I have it counting the words, and it seems to go through but it is not checking to see if the words are good, bad or indifferent.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't change earlier posts after you have had replies as it can make the posted replies look like nonsense to other members.

As I have already said StopCoding and work out how to do it using pencil and paper.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:

(I have updated the code above)


1) I get the following:
2) Enter the text with spaces(press enter to done):
3) Good and Bad
4) The phrase is: Good and Bad
5) Phrase is 3 words long.
6) 0 0
7) Line:
8) 0P0N

It should come back with 1 1 on line 6 and 1P1N on line 8. I have it counting the words, and it seems to go through but it is not checking to see if the words are good, bad or indifferent.


There is a very simple issue. Just check the line where you check for the equality and see of you have used the correct equals method! As a hint, is Java equals java?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:There is a very simple issue. Just check the line where you check for the equality and see of you have used the correct equals method! As a hint, is Java equals java?

It's not quite that simple as that as the OP is comparing an array with a string for equality, which of course will never be equal.

@ John Morgan: Rather than "is this array equal to this word" you should be saying "does this array contain this word". There are several ways of doing this such as using a Set rather than an array but if you want to use an array you can either iterate over it each time or if the array is going to get very long you could use the Arrays class to initially sort() it and then do a binarySearch(). But which ever way you do it you still need to take into account the point raised by Joe.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:

@ John Morgan: Rather than "is this array equal to this word" you should be saying "does this array contain this word". There are several ways of doing this such as using a Set rather than an array but if you want to use an array you can either iterate over it each time or if the array is going to get very long you could use the Arrays class to initially sort() it and then do a binarySearch(). But which ever way you do it you still need to take into account the point raised by Joe.


Sorry that was my mistake, I just overlooked his code. Converting an array to a collection and then checking for contains would be the way to go, something like:


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Converting an array to a collection and then checking for contains would be the way to go, something like:

It's certainly one way of doing it but converting it to a HashSet would be better as the contains method is likely to be more efficient (assuming there will be more than just a couple of objects and those objects have a reasonable hashCode() implementation, which String does).
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:
Tony Docherty wrote:

@ John Morgan: Rather than "is this array equal to this word" you should be saying "does this array contain this word". There are several ways of doing this such as using a Set rather than an array but if you want to use an array you can either iterate over it each time or if the array is going to get very long you could use the Arrays class to initially sort() it and then do a binarySearch(). But which ever way you do it you still need to take into account the point raised by Joe.


Sorry that was my mistake, I just overlooked his code. Converting an array to a collection and then checking for contains would be the way to go, something like:




Yup, that is what I was told last night. I have a friend who is a C programmer he put me on to the fact that I was trying to take a word and compare it to a whole list (not just one word). Once I got that I just created a loop to look through the list comparing it to the word. Not the best way of doing it but it works and this is only lesson 5/86 .... The KISTS rule.... Keep It Simple To Start.

Thanks for all the input.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:Yup, that is what I was told last night. I have a friend who is a C programmer he put me on to the fact that I was trying to take a word and compare it to a whole list (not just one word). Once I got that I just created a loop to look through the list comparing it to the word. Not the best way of doing it but it works and this is only lesson 5/86 .... The KISTS rule.... Keep It Simple To Start.

This is why I was telling yesterday to stop coding and write out the solution to the problem in your native language because then when you convert from that to code you wouldn't have made that type of mistake.
I was avoiding just telling you what to do to fix your code as that doesn't teach you how to do it properly next time.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
This is why I was telling yesterday to stop coding and write out the solution to the problem in your native language because then when you convert from that to code you wouldn't have made that type of mistake.
I was avoiding just telling you what to do to fix your code as that doesn't teach you how to do it properly next time.


My problem was that I did not understand that I was looking at an array as a whole (I thought it stepped through the array one word at a time). No amount of writing it down on paper would have helped that (which I did do originally as notes in my code). Once I understood that I was looking at the array as a whole, and not each word in the array, I was able to fix the problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:My problem was that I did not understand that I was looking at an array as a whole (I thought it stepped through the array one word at a time).

IMO, it's a pity that your friend gave you the answer because it's a far more basic issue than the logic you were trying to implement.

Indeed, it has nothing whatsoever to do with the problem you were trying to solve; it's a simple syntax error - and unless you remember precisely what you did wrong (and why it was wrong), you're likely to repeat it.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't suggesting writing it down would solve the coding problem you had. My suggestions were to write out the solution in fine detail and then change those detailed notes to code ie forget what you had already done and start again. If you had written out in detail how to search a list of words for a specific word you wouldn't have written the code you wrote.
Just to clarify notes in code tend to say something like "search array for word" and this is not what I was meaning by detailed notes. For a start using the word 'array' is computer language and binds your solution to a specific implementation and it doesn;t give anywhere near enough detail for you to safely code it. Your notes should be based on how you would do it and not how you think you should write software to do it. For example you could have written something like:

For each word in the sentence check to see if it is in the bad or good list of words as follows:
Take the first word in the sentence and compare it to the first word in the bad word list. If they are not the same, ignoring case, then compare it to the next word in the bad word list. Continue doing this until either a match is found or there are no more words in the bad word list. If the word was not found in the bad word list ....
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I wasn't suggesting writing it down would solve the coding problem you had. My suggestions were to write out the solution in fine detail and then change those detailed notes to code ie forget what you had already done and start again. If you had written out in detail how to search a list of words for a specific word you wouldn't have written the code you wrote.
Just to clarify notes in code tend to say something like "search array for word" and this is not what I was meaning by detailed notes. For a start using the word 'array' is computer language and binds your solution to a specific implementation and it doesn;t give anywhere near enough detail for you to safely code it. Your notes should be based on how you would do it and not how you think you should write software to do it. For example you could have written something like:

For each word in the sentence check to see if it is in the bad or good list of words as follows:
Take the first word in the sentence and compare it to the first word in the bad word list. If they are not the same, ignoring case, then compare it to the next word in the bad word list. Continue doing this until either a match is found or there are no more words in the bad word list. If the word was not found in the bad word list ....

Gotcha.. I tend to think and write in short hand (I wrote down something to the effect of "Look in list to see if any of the words match the first, then second, etc words in the sentence"). now the light bulb goes on. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!