0

let say i have fallowing javascript function-

function isDigit (c)
{ return ((c >= "0") && (c <= "9"))
}
function isAlphabet (c)
{
return ( (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") )
}

How can i write same thing in java. Thanks.

0

3 Answers 3

6

Use java.lang.Character class methods.

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

5 Comments

@AndreyAdamovich : Links are for jdk 5 update them for jdk 6.
@Harry, methods didn't really change since that and I can't really edit my comments :)
@Andrey For tips on getting a link to the latest JavaDocs, see point 2.
Worth nothing that the Character equivalents handle all character sets. e.g. 208 characters are digits.
3

Respectively:

java.lang.Character.isLetter(c);
java.lang.Character.isDigit(c);

But if you want to make your own implementations:

boolean isAlpa(char c) {
    return c >= 'A' && c <= 'Z'; /* single characters are
            enclosed with single quotes */
}

Comments

3
public boolean isDigit(char c) { 
  return ((c >= '0') && (c <= '9'));
}

public boolean  isAlphabet(char c) {
  return ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') );
}

Comments

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.