1

Whenever I am trying to lookup on the internet to see if an int value which is accepted from a userfield has some characters like A-z or any special characters, all the sites happen to presume that the input is in the form of String. Function

boolean numberValidation(int number){
    if number has characters mentioned above, return false
   else return true
}
3
  • Hint: number has type int, it can't contain alphanumeric characters. Commented Aug 28, 2014 at 11:56
  • How can an int have characters in it? Impossible. You must have meant String. In that case, regular expressions are one way to do it. Commented Aug 28, 2014 at 11:56
  • I cannot understand your problem. Integers are numeric representated and characters(also numbers) alphanumeric.clarify it: You have an inputfield which accepts alphanumeric inputs but you only want to allow the numeric ones in it? Commented Aug 28, 2014 at 11:59

4 Answers 4

2

In that case, the compiler will complain about the parameter passed.

If the parameter was a String and you want to detect if the String is a valid integer, you can parse it controlling exceptions. The following code is a suggestion:

boolean numberValidation(String number){
    try{
        Integer.parseInt(number);
        return true;
    }catch(NumberFormatException e){
        return false;
    }
}

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

Nedless to say that it can be done with regex too, but it is a bit complex way (but a cool way :)).
1

If you're looking for hexadecimal numbers with any digit > 9, you can do it as such:

// hex representation of -889275714
int i = 0xcafebabe;
// just 1
int j = 1;
// represents both as hexadecimal String representations
String is = Integer.toHexString(i);
String ij = Integer.toHexString(j);
// pattern to check for letters
Pattern alpha = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
System.out.printf("\"%s\" contains characters, \"%s\" does not.%n", is, ij);

Output

"cafebabe" contains characters, "1" does not.

Note

For Strings representing numbers, as shown in other answers, use Integer.parseInt and catch a NumberFormatException.

2 Comments

Can you please tell me how to use the pattern alpha to check against integer. Something similar to Boolean valid = password.matches(passwordRegex); ?
@Kernelfreak not sure what you mean? Maybe a new question might be more suitable for this?
1

Try to parse it and if exception occurs return false else return true

boolean numberValidation(String supposedNumber) {
    boolean flag = false;
    int x;
    try {
        x = Integer.parseInt(supposedNumber);
        flag = true;
    } catch(NumberFormatException ex) {
        flag = false;
    }
    return flag;
}

5 Comments

1. It doesn't compile, 2. Just-code answers won't help the OP.
Don't see why this would be downvoted. Perfectly acceptable, although I'm not crazy about using exceptions this way.
@Nabin catch without exception?
@BackSlash You must be kidding me.. You look for code accuracy too?
@BackSlash I am not quite sure if the mentioned exception is spelled correct :-)
0

Here's what I think you want:

public boolean isNumber(String value) { 
    return Pattern.compile("\\d+").matcher(value).matches();
}

1 Comment

Why not just value.matches("\\d+")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.