• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing strings  RSS feed

 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the if statement is never true even if "word" and "Spelled" actually do match.
I was thinking i could convert them into char arrays then comparing them or another possible solution if you guys have any.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the if statement is never true even if "word" and "Spelled" actually do match. 

Add a print statement just before the if statement that prints out the values of the two variables being compared.  Execute the code, copy the output and post it here so we can see what you are talking about.
Be sure to delimit the Strings  (">"+theString +"<") so any extra spaces would be shown.
equals() should work.
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
the if statement is never true even if "word" and "Spelled" actually do match. 

Add a print statement just before the if statement that prints out the values of the two variables being compared.  Execute the code, copy the output and post it here so we can see what you are talking about.
Be sure to delimit the Strings  (">"+theString +"<") so any extra spaces would be shown.
equals() should work.


(Spelled is a word from the dictionary that is in a text file) it goes through the entire while loop in Checker but it does not go back to countWords. I can only show a snippet since there are over 400,000 words that are compared. Screen-reader is the first word (word variable) but it does not get the second element.
word is:Screen-reader     Spelled is:Zanthoxylum
word is:Screen-reader     Spelled is:zanthoxylum
word is:Screen-reader     Spelled is:Zantiot
word is:Screen-reader     Spelled is:zantiote
word is:Screen-reader     Spelled is:Zantos
word is:Screen-reader     Spelled is:ZANU
word is:Screen-reader     Spelled is:Zanuck
word is:Screen-reader     Spelled is:zany
word is:Screen-reader     Spelled is:zanyish
word is:Screen-reader     Spelled is:zanyism
word is:Screen-reader     Spelled is:zanyship
word is:Screen-reader     Spelled is:zanza

This is the output if the method is not called so only the variable word is printed
Screen-reader
users,
click
here
to
turn
off
Google
Instant.
Gmail
Images
Sign
in
Google
Instant
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are the Strings that you think should compare as equal (match) in the if statement?

there are over 400,000 words that are compared.

You need to reduce that to a few hundred for testing.
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Where are the Strings that you think should compare as equal (match) in the if statement?

there are over 400,000 words that are compared.

You need to reduce that to a few hundred for testing.


This is a html spellchecker that gets words from the html body and spell checks those words. the variable word is assigned to it. in this program I am using google.com
The spelled variable is from a text file that contains every word in the english library.
 
Rajith Pemabandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried with function on the word. Further and variables are not defined in you sample code
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think the equals() method is not working?

Can you post some proof?
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Why do you think the equals() method is not working?

Can you post some proof?

output when the function Checker is not called.


nothing prints when function is called without sample ouputs and the only output is in the if statement.

this prints when there is a sample output in the Checker while loop but not in the if statement.


 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajith Pemabandu wrote:have you tried with function on the word. Further and variables are not defined in you sample code


text and counter are relating to what is wrong its related to something else.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post some output showing Strings that you think should match but were not found with the equals() method?

Nothing you have posted yet shows what you are asking about.

Note:  It doesn't make sense to test a program with 400,000 items until it has been tested with just hundreds.

word is:Screen-reader
That is the only value of word that has been posted.
When does the value of word change?  What is it compared against after its value has changed?
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Can you post some output showing Strings that you think should match but were not found with the equals() method?

Nothing you have posted yet shows what you are asking about.

Note:  It doesn't make sense to test a program with 400,000 items until it has been tested with just hundreds.

word is:Screen-reader
That is the only value of word that has been posted.
When does the value of word change?  What is it compared against after its value has changed?


So i made the code a bot more consise so it is easier to read:


Basically the outter most loop only happens once when there is the (while(Scan.hasNext()) but if i comment out the inner while and if loop so that there is only while st.hasMoreTokens() it gets all the words from the html and prints them
ex nothing commented out:

Screen-reader

but when the inner while and if statement are gone with the code looking like this:

it outputs:
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want it to go through all the words for word and Spelled
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what the code is doing? It gets some Strings from the file: ListOfWords.txt
and some more Strings from the String: text

Then what is it supposed to do with those two sources of Strings?

If it is to check if the Strings from text are in the ListOfWords.txt file, it should load all the Strings in ListOfWords into a Set and then test if the next String from text is in that Set.


Have you ever tested the code with a small number of words so that the printout will not be over a few hundred lines?   You need to do that.
 
Sunny Sandu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Can you explain what the code is doing? It gets some Strings from the file: ListOfWords.txt
and some more Strings from the String: text

Then what is it supposed to do with those two sources of Strings?

If it is to check if the Strings from text are in the ListOfWords.txt file, it should load all the Strings in ListOfWords into a Set and then test if the next String from text is in that Set.


Have you ever tested the code with a small number of words so that the printout will not be over a few hundred lines?   You need to do that.


dont worry i got it i just had to bring the pointer back to the start of the file, but thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!