• Post Reply Bookmark Topic Watch Topic
  • New Topic

small spell checker program  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well im working on this program that gives the meaning of a user entered abbreviation such as if a user enters lol it will display lol = laughing out loud,
i also want this to be able to guess what the user is entering so if a person enters LLo it will say did you mean lol = laughing out loud

heres what i have so far



im trying to use else if (origTweet.indexOf("l", origTweet.indexOf("l") + 1))){

}
and just nest the words in one but i cant even test it because im getting a expected "(" token when i try to add another if statement. blah\

well the nested indexOf is saying theres it cant convert a boolean to a int?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:im getting a expected "(" token when i try to add another if statement.
im trying to use
else if (origTweet.indexOf("l", origTweet.indexOf("l") + 1)))

Count parentheses (opening and closing), must be there problem with this specific issue.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i = origTweet.indexOf("l");
else if (origTweet.indexOf("l", origTweet.indexOf("l", i + 1)) != 1){
System.out.println("Did you mean 'LOL' Laughing out loud?");
}
well i got it to work but when i enter a && or a || condition in the () of the if statement it comes out with a error.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code with the error and print the error message.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well just the way it is now except i changed the scnr.next() to scnr.nextLine() but when i enter something like "hello lol" it goes straight to "did you mean lol = laughing out loud" the only way it just goes to lol = laughing out loud is if the first word is lol.
but anyway as you can see the second lol if statement only has the two L's so when i try to ad && or || O it gets this error (Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator || is undefined for the argument type(s) int, boolean

at testIf.main(testIf.java:17))

heres the code with trying to add the "o" i guess i could try adding another nested indexOf()

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also my origTweet.toLowerCase() is not working


edit: ok its working...sort of the toLowerCase() but i think the condition in the indexOf() is not because it only works when i put the lol in upper case
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
well im working on this program that gives the meaning of a user entered abbreviation such as if a user enters lol it will display lol = laughing out loud,
i also want this to be able to guess what the user is entering so if a person enters LLo it will say did you mean lol = laughing out loud


The key to performance is elegance, not battalions of special cases.

Thus your code can become simpler by removing the if else constructs, and by separting the part of the function that reads input from the part of it that computes the result.
Here is a modest try on refactoring:


Having said that, a solution to your question could be:
1) storing the strings you do know how to translate in some objects.
2) for each new input you iterate the objects of known translatable strings and you compute a distance (an int number) between current input and that known object
3) you use the object with the smallest distance as the suggestion for the user

I shall write the code for my idea in hope you understand it better than words, but please bear in mind that I will not test it and it is up to you to debug it:



If you are interested in algorithm that tell you how much String "abcd" is closer to "abcc" than to "aaaa" , search the net on string distance algorithm.
But the point of the code above is to show you how much of a difference makes initial structuring your code to obtaining a solution. Hope it helps.


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get much better performance from a switch than multiple ifs. I would prefer to find myself a data structure which allows you to match the abbreviation to its expansion. There is one ready‑made in the Collections Framework. Then you don't need to hard‑code the abbreviations; you load them from a resource fileI know nothing about writing spellchecks.
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You get much better performance from a switch than multiple ifs


Do you happen to know if the java case matching mechanism is better than multiple if because teh compiler sorts the possible cases then makes a binary search on it, or maybe some other optimization like this ?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote:
I shall write the code for my idea in hope you understand it better than words, but please bear in mind that I will not test it and it is up to you to debug it:

Nice work. Have a cow.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know guys thanks though i can only change certain parts of the code because im using this online course that teaches you and they havent covered switch statements so i could only use what theyve taught so far. thanks though!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!