• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to write thousand if else shortly?  RSS feed

 
sindy lee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

i have same statement in if-else:

if(rday.equals("21.08.2009")==true)
{
_AppearancePubli.add(publ);
_htrday.put(rday, _AppearancePubli);
}
else if(rday.equals("22.06.2010")==true)
{
_AppearancePubli1.add(publ);
_htrday.put(rday, _AppearancePubli1);
}
else if(rday.equals("01.10.2010")==true)
{
_AppearancePubli2.add(publ);
_htrday.put(rday, _AppearancePubli2);
}
else if(rday.equals("25.12.2010")==true)
{
_AppearancePubli3.add(publ);
_htrday.put(rday, _AppearancePubli3);
}
... ... ...

it works so fine, but what can i do, if there are thousand dates ("dd.mm.yy")? I can't write it always like this. (_AppearancePublixx is
Vector datatyp, _htrday is Hashtable), and i can't define so many endless vectors neither. Do you have any idear?

Thanks
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you certainly don't want to copy and slightly modify your code 1000 times. ;-)

either use a database, or maybe something like a xml-file (a bit hard to guess without further information). Don't hard code this configuration(?) stuff...

Also your naming scheme is a bit unusual. Starting a variable name with "_" is debatable, but try to stick to non-capital letters to name a variable ("appearancePublic")

cheers,
Jan
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jan. Use a database or load it from a file into a datastructure such as a Set.

Also don't write == true. It's unnecessary. Don't use abbreviations. It's only confusing and your ide will type those extra characters.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard-coding dates like that will lead to brittle code which is very difficult to maintain.
 
sindy lee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the tips. I'm trying with the XML file
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Groth wrote:Starting a variable name with "_" is debatable


Not to be pedantic, but according to the official Java naming conventions, that's a no-no. From here: "Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed."
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:
Jan Groth wrote:Starting a variable name with "_" is debatable


Not to be pedantic, but according to the official Java naming conventions, that's a no-no. From here: "Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed."


I agree with 'debatable' The official Java naming conventions are only a recommendation and you are free to add, modify or remove stuff as you see fit. Many companies do.
If you want your career with a company to last, you should always follow their style guides when they differ from the standard Java one.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Kevin Workman wrote:
Jan Groth wrote:Starting a variable name with "_" is debatable


Not to be pedantic, but according to the official Java naming conventions, that's a no-no. From here: "Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed."


I agree with 'debatable' The official Java naming conventions are only a recommendation and you are free to add, modify or remove stuff as you see fit. Many companies do.
If you want your career with a company to last, you should always follow their style guides when they differ from the standard Java one.


Fair enough. But when you're unsure, or when your company has no style guides, or if you're programming on your own, it's good to at least be aware of what the official code conventions are. I was merely showing the OP that they existed.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:Fair enough. But when you're unsure, or when your company has no style guides, or if you're programming on your own, it's good to at least be aware of what the official code conventions are. I was merely showing the OP that they existed.


I quite agree. I just wanted to be sure the OP wasn't given the impression that the standard conventions are set in stone and any divergence would result in instant transferral to a C# project
 
Vadym Baranenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you should buy a book of algorithms, it can help you a lot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!