May 24, 2012

Requirement: We have to findout Employee Number in the given String and It is must be Number only. If Employee Number is not Number, It shows "Employee Number is not Integer.".

public class EmpNoIsInteger {

public static void main(String[] args) {
String givenString = "ENAME=Myjavacafe~*ENO=9999~*";
String empNo = "*ENO";
// 01234
boolean empFlag = givenString.contains(empNo);
if (empFlag == true) {
String[] givenWords = givenString.split("~");
for (String givenWord : givenWords) {
if (givenWord.startsWith(empNo)) {
String givenEmpNo = givenWord.substring(5,
givenWord
.length());
System.out.println("Given Employee Number:" +
givenEmpNo);
boolean sFlag = isInteger(givenEmpNo);
if (sFlag == true) {
System.out
.println("Employee Number("
+givenEmpNo+") is Valid(It is Integer values)");
} else {
System.out
.println("Employee Number("
+givenEmpNo+") is Invalid(It is not Integer values).");
}
}
}
} else {
System.out.println("Employee Number is not avaliable
given String.");
}

}

public static boolean isInteger(String input) {
try {
Integer.parseInt(input);
return true;
} catch (Exception e) {
return false;
}
}

}

Output:

Scenario 1: Given Employee Number only Numbers(9999)

Given Employee Number:9999
Employee Number(9999) is Valid(It is Integer values)

Scenario 2: Given Employee Number is Alphanumeric(9999SS)

Given Employee Number:9999SS
Employee Number(9999SS) is Invalid(It is not Integer values).

Scenario 2: Given Employee Number is NULL

Given Employee Number:
Employee Number() is Invalid(It is not Integer values).

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts