• Post Reply Bookmark Topic Watch Topic
  • New Topic

issue while creating an object  RSS feed

 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just started exploring Java.  Sorry if I am asking a silly quetion.
I have created a class to hold two arrylists.  First list to hold words.  Second list to hold meaning of the words.  And a setter method addWord.



From another class I am creating an object of ListOfWords and trying to add words -



In this class, I am getting error "Syntax error on token "addWord", invalid AnnotationName" at 2nd statement w1.addWord.......

Can you please help me to resolve this. 
I was just trying to build a simple dictionary to hold words and their meanings.  And, from main class, find a word and show its meaning.

Thanks,
-Anand

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is illegal to have such expression outside of a constructor, a method or an initializaion block.

Also, having parallel lists is very error-prone if you manage to desync them.
I suggest using a map instead:
 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pawel thanks for the response.  I will explore hashmap approach as well.

I tried with below appraoch, i.e. to build list of words using a local method like this (it also has a method which searches for word) and it works fine.  Your feedback on how good or how bad this approach will really help me improve my coding.  Thanks.




From a wrapper class I am trying to print the meaning by searching the word. 




Thanks,
-Anand

 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am converting the string to lower before sending to search.  If the wordToSearch = "ready", it gives the result.  If I make it "Ready", it is not searching even though I convert it to lower.  Can you please help.








Result -

Inside search method: word = ready
Ready =

 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String should not be compared like this



it should be

 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok.  Thanks.  It works.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the variable should be named word (not words) as it represents a single word.
 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes true.  Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!