• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with the code  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Suppose i have a file called as country.java


i have such 100 variables in Country.java

and i have another file Main.java

now the logic i want to put in main method of Main.java is


i mean if i want to put logic as follows: if a is in(any of the variables in country.java) go ahead and print a or exit. Is it possible to avoid so many or conditions and just have one condition.

Hope, the question is clear.

Thanks,
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob,
Do you need to have lose variables? If you can put them in a set and call set.contains(), that will be simpler. The other alternative is to use reflection to go through all the fields in that class.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the functionality needed, it looks like an enum would be a good alternative too.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note, calling new String("a string") is redundant.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to have lose variables...meaning i cannot touch Country.java because that is already coded...i just need to check that whatever parameter i enter..does it belong to country.java...but i just wanted to know is it possible to avoid so many ors...meaning can i do a comparison using some other thing
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as Jeanne said you could put the Strings you wanted to compare to into a Set (a HashSet would be a good choice) and then just call the method which asks whether the String you have there is in that Set.

That Set wouldn't have to be in the Country class. It could be in the class which contains the code you are writing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!