2

If you make a variable type String you can still input numbers.

Example:

"Enter name: 123"

The name is still accepted. How would I make it so a variable name only accepts letters, otherwise an error message will appear?

dan
  • 127
  • 9

5 Answers5

5

You cant necessarily limit the user input to just numbers, however you can validate that input to ensure that it meets your expectations. For example, if you want to ensure that your string does not contain any numbers you can do:

if(str.matches(".*\\d.*")){
   // contains a number
} else{
   // does not contain a number
}
ConMan
  • 1,642
  • 1
  • 14
  • 22
1

I don't know any native function to do this, buy you can simply read the string (char per char) and ask if the value is between 65 and 90 (capital letters) or 97 and 122 (normal letters).

The code would be:

    String string = "ABCD";
    boolean hasOtherCharacters = false;
    for ( int i = 0; i < string.length();i++ )
    {
        if ( !(string.charAt(i) >= 65 && string.charAt(i) <= 90) && !(string.charAt(i) >= 97 && string.charAt(i) <= 122) )
        {
            hasOtherCharacters = true;
            break; 
        }
    }

    if ( hasOtherCharacters )
    {
        //WHATEVER YOU WANT

WARNING: This solution applies if your character set is between those ASCCI range.

Nico
  • 309
  • 1
  • 3
  • 18
  • what if the user input ç or ñ? – André Jul 03 '15 at 14:04
  • That depends your language, just extend the implementation in the if statement. – Nico Jul 03 '15 at 14:06
  • Good luck just *extending*. This solution works if input is plain ASCII, if you need unicode, going with this is just reeinventing the wheel. http://stackoverflow.com/questions/12187645/java-utf8-encoding-char-string-types – André Jul 03 '15 at 14:12
  • There are only two more conditions for your answer, and the questions is asking only for letters. – Nico Jul 03 '15 at 14:17
  • Fair enough, will retract my downvote, if you add this caveat to your answer – André Jul 03 '15 at 14:19
1

Without the help of any additional library you just use Javas pattern matcher:

Pattern p = Pattern.compile("\\D*");

String test1 = "abc";
String test2 = "abc123";

Matcher m1 = p.matcher(test1);
System.out.println("does test1 only contain letters? " + m1.matches());         
//prints true

Matcher m2 = p.matcher(test2);
System.out.println("does test2 only contain letters? " + m2.matches());
//prints false
zeisi
  • 5,520
  • 2
  • 22
  • 21
0

If using a JTextField or something that allows listeners instead of just the command line, you can prevent the user from inputting numbers with a KeyListener:

userEntry.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent e) {
        if (Character.isDigit(e.getKeyChar())) {
            e.consume();
        }
    }
});
JDrost1818
  • 1,116
  • 1
  • 6
  • 23
0

You could check to see whether the string is numeric using a lambda expression:

String myString = "abcdef1234567890";

if (myString.chars().allMatch( Character::isDigit ))

Will output True if the string only contains numbers

  • I think the questioner is asking for it to be rejected if ANY characters are digits rather than ALL characters are digits. – zudduz Jul 04 '17 at 19:41