Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to code complex conditions

802930Oct 10 2011 — edited Oct 19 2011
Hi Everyone,

First, please forgive if I don't use programming terminology correctly, I am a beginner with no background in informatics.

My question will be a general one: What is the good approach to code complicated conditions?

I have a good example to start with which is actually a concrete task that I need to do in my small application:

TODO: Shorten/abbreviate a person's full name to fit in to a 16 characters.
For example: Johnathan Necromancer --> J Necromancer

There are many possibilities how to do it based on the length of the first, last and middle names and there is an order of preference for these solutions.
So if possible abbreviate only the first name like above; if it's still too long then then leave the space out, and so on. These would be the preferences demonstrated with examples:
1. J L Smith
2. JL Smith
3. JLSmith
4. John Languely S
5. John Lang B S
6. John L S
7. JohnLS
8. J L S
9. JLS

How to code it? With nested if -s or with if-else blocks or in an other way?

First I did it with nested if -s, but it was kind of mess after a while, so the most straightforward way for me was to do it as follows:
(Simply 'calculate' the abbreviated names in the preferred order and return if it fits into the 16 characters.)
if (displayName.length() > 16) {

			String[] dn = displayName.split("\\s+");
			String lName = dn[dn.length - 1];
			StringBuffer dName16 = new StringBuffer();
			
			// J L Smith
			dName16 = new StringBuffer();
			for (int i = 0; i < dn.length - 1; i++) {
				dName16.append(dn.charAt(0) + " ");
}
dName16.append(lName);
if (dName16.length() <= 16){
return new String(dName16);
}

// JL Smith
dName16 = new StringBuffer();
for (int i = 0; i < dn.length - 1; i++) {
dName16.append(dn[i].charAt(0));
}
dName16.append(" " + lName);
if (dName16.length() <= 16){
return new String(dName16);
}

// JLSmith
dName16 = new StringBuffer();
for (int i = 0; i < dn.length - 1; i++) {
dName16.append(dn[i].charAt(0));
}
dName16.append(lName);
if (dName16.length() <= 16){
return new String(dName16);
}

//more code..
I would like to know how to approach these kind of situations effectively and clearly in the least error prone way. How to code complex conditions? Is there any book or theory about it?
I am not sure it is clear what I am asking, but I will follow up with more description on what I mean later. But please comment, ask, advice anything in the meantime - I would really appreciate any help with this.

Thank you in advance!
lemonboston                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
This post has been answered by TPD-Opitz on Oct 14 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2011
Added on Oct 10 2011
19 comments
532 views