Link to home
Start Free TrialLog in
Avatar of Michelle Aycoth
Michelle Aycoth

asked on

Basic Java Case or If-Else statement...

In reading, it's my understanding that you cant use strings in Case-switch statements until after Java 7.0.   (We are running Java 6.0).   Which brings me to my next point, I'm trying to run an If-Else If statement using a substring.   The point of the code snippet is to determine if we received partial information on the ID and add the necessary prefix to it, if needed.   Disregard all the log.info statements.   I was trying to determine where it was failing.    

Do I need a break statement to make sure none of the other else if statements are true?
Is my code to add the prefix to the string correct?
			user_ID = splitIndexFileData(indexFileData);	
			String str = user_ID.substring(0,1);
			String str1 = user_ID.substring(0,2);
			
			log.info(str);
			log.info(str1);
			
			if ((user_ID.equalsIgnoreCase(""))||(user_ID.isEmpty())) {
				user_ID = "99101010101";
			}else if ((user_ID.substring(0,1) == "0")||(user_ID.substring(0,1) == "2")||(user_ID.substring(0,1) == "3")){
			    	user_ID = splitIndexFileData(indexFileData);
			    	log.info(user_ID);
			}else if ((user_ID.substring(0,2) == "UN")||(user_ID.substring(0,2) == "NL")||(user_ID.substring(0,2) == "ND")){
					user_ID += ""+ 30 + splitIndexFileData(indexFileData);
					log.info(user_ID);
			}else if ((user_ID.substring(0,2) == "UF")||(user_ID.substring(0,2) == "FF")){
					user_ID += ""+ 26 + splitIndexFileData(indexFileData);	
					log.info(user_ID);
			}else if (user_ID.substring(0,1) == "5"){
					user_ID += ""+ 06 + splitIndexFileData(indexFileData);	
					log.info(user_ID);
			}else if (user_ID.substring(0,1) == "X"){
					user_ID += ""+ 04 + splitIndexFileData(indexFileData);
					log.info(user_ID);
			}else if ((user_ID.substring(0,1) == "I")||(user_ID.substring(0,1) == "E")||(user_ID.substring(0,1) == "D")||(user_ID.substring(0,1) == "4")||(user_ID.substring(0,1) == "1")||(user_ID.substring(0,1) == "U" && str1 != "UN")){
					user_ID += ""+ 01 + splitIndexFileData(indexFileData);	
					log.info(user_ID);					
			}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For efficiency, only call the substring function once as needed. You are already getting the results and storing them into variables:

String str = user_ID.substring(0,1);
String str1 = user_ID.substring(0,2);

Open in new window


Now that you have them saved, you don't need to call substring again, like you are here:

}else if ((user_ID.substring(0,2) == "UF")||(user_ID.substring(0,2) == "FF")){

Open in new window


Instead, reuse the variables you have above, as such:

} else if (str1.equals("UF") || str1.equals("FF"))) {

Open in new window

Avatar of Michelle Aycoth
Michelle Aycoth

ASKER

Thank you for the assist.   I see the error of my ways and understand the difference now between using == and .equals method.   Appreciate the quick response.