May 24, 2012

Requirement: We have to findout EmpName in the given string and It is must Alphabet only. If EmpName is not Alphabet it shows "EmpName is not Alphabet".

public class ENameIsAlphabet {
public static void main(String[] args) {

String sOne = "Emp NO: 100*&Emp Name: Myjavacafe123*&Emp Sal:
200*&";
String[] sArray = sOne.split("&");
for (String s : sArray) {
String eName = "Emp Name:";
// 012345678910
if (s.startsWith(eName)) {
String sTwo = s.substring(10, s.length() - 1);
isString(sTwo);
}
}
}

public static void isString(String s) {
boolean isFlag = false;
String charSet =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (s.length() > 0) {
ArrayList<Character> invalidCharList = new
ArrayList<Character>();
for (int i = 0; i < s.length(); i++) {
if (charSet.indexOf(s.charAt(i)) == -1) {
invalidCharList.add(s.charAt(i));
isFlag = true;
}
}

if (isFlag == true){
System.out.println("Employee Name contains invalid
Character(s) (It is not Alphabet): "+invalidCharList.toString());
System.out.println("Given Employee Name("+s+") is
not a Alphabet.");
}
else
System.out.println("Employee Name("+s+") is a
Alphabet.");
}else{
System.out.println("Employee Name("+s+") is Null in Given
String.");
isFlag = false;
}
}
}


Output:


Scenario 1: Given Employee Name only Alphabets

Employee Name(Myjavacafe) is a Alphabet.

Scenario 2: Given Employee Name contains alphanumeric

Employee Name contains invalid Character(s) (It is not Alphabet): [1, 2, 3]
Given Employee Name(Myjavacafe123) is not a Alphabet.

Scenario 3: Given Employee Name only Numbers

Employee Name contains invalid Character(s) (It is not Alphabet): [1, 2, 3,
4]
Given Employee Name(1234) is not a Alphabet.

Scenario 4: Given Employee Name is NULL

Employee Name() is Null in Given String.

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