on 10-13-2011 4:37 PM
Hello All,
Can some one tell me how to check whether the Input coming is String or Alphanumeric
Regards,
Moorthy
Hi Moorthy,
There is no standard mapping function to check the alphanumeric. You need to write a small UDF to do this function.
here is small code: this UDF takes one input(inString) value for which we check whether it is numeric or alphanueric
Boolean result = false;
try
{
Long.parseLong(inString);
result = true;
return result.toString();
}
catch(Exception e)
{
result = false;
return result.toString();
}
Above UDF gives true is inString is Numeric or false if it's not numeric (it means, inString is Alphanumeric)
Thanks,
RK
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Other alternate options ..
1 : use a regular expression (a more elegant solution).
public static boolean isNumeric(String inputData) {
return inputData.matches("[-+]?\\d+(\\.\\d+)?");
}
2:use the positional parser in the java.text.NumberFormat object (a more robust solution). If, after parsing, the parse position is at the end of the string, we can deduce that the entire string was a valid number
public static boolean isNumeric(String inputData) {
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(inputData, pos);
return inputData.length() == pos.getIndex();
}
3: use the java.util.Scanner object. Very useful if you have to scan multiple entries.
public static boolean isNumeric(String inputData) {
Scanner sc = new Scanner(inputData);
return sc.hasNextInt();
}
Scanner also has similar methods for longs, shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well as methods for integral types where you may input a base/radix other than 10 (10 is the default, which can be changed using the useRadix method).
one more option...
input : var1
execution type: single value
for (int i = 0; i < var1.length(); i++)
{
if(Character.isDigit(var1.charAt(i)))
return "input is alphanumeric";
}
return "Input is string";
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.