1

i am not able to check the non string value such as a-z and A-Z using public method

Scanner input = new Scanner(System.in);
System.out.print("Enter your First Name: ");
String First_name = input.nextLine();


Scanner last = new Scanner(System.in);
System.out.print("Enter your Last Name: ");
String Last_name = last.nextLine();

here is public method

public static boolean check_non_string_value (String First_name, String Last_name){
  // dont know how to check the input here
} 
5
  • Check out regular expressions. Commented Oct 9, 2013 at 13:37
  • What are you checking? Commented Oct 9, 2013 at 13:37
  • I think you mean non alphabetic character instead of non string value. Commented Oct 9, 2013 at 13:38
  • search about regular expressions and pattern matching and also string tokenizer Commented Oct 9, 2013 at 13:41
  • 2
    I would like to point out that limiting valid first and last names to only contain A-Z is questionable in all but the most insultingly narrow-minded contexts. Commented Oct 9, 2013 at 13:43

2 Answers 2

3

You can use regular expressions but it's also relatively simple to just loop over each string yourself and use Character.isLetter() to check each character:

for (char c : firstName.toCharArray()) {
    if (!Character.isLetter(c))
        return false;
}

for (char c : lastName.toCharArray()) {
    if (!Character.isLetter(c))
        return false;
}

return true;

Note that I've used more conventional names: firstName and lastName. You should always try to follow Java's naming conventions (i.e. use camelCase). Your method name should also be checkNonStringValue.

With regular expressions, you could do something like:

private static final Pattern letters = Pattern.compile("\\p{Alpha}+");

...
    return letters.matcher(firstName).matches() &&
           letters.matcher(lastName).matches();

\p{Alpha} is a pre-defined character class meaning [a-zA-Z]. See Pattern for more details.

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

Comments

1

This method valid for a-z and A-Z characters

public static boolean check_non_string_value (String First_name, String Last_name){
   if(First_name.matches("[a-zA-Z]+")&&Last_name.matches("[a-zA-Z]+")){
          system.out.println("Valid input");
        return true;
   }
   return false;
}  

This method valid for all word characters

public static boolean check_non_string_value (String First_name, String Last_name){
   if(First_name.matches("\\w")&&Last_name.matches("\\w")){
          system.out.println("Valid input");
        return true;
   }
   return false;
}  

4 Comments

This matches only a single letter.
\wis equivalent to [a-zA-Z_0-9] so you will match my_nam3 but not Jérémy.
No need for [\\w]. \\w is enough. Also this will also match digits and _.
Well there are still issues here. 1. You need to return a boolean from both of these methods. 2. It would be a much better idea (especially if this method will be called often) to pre-compile this regex so as to avoid the redundancy.

Your Answer

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