0

In my if statement I would like to somehow check if the user's input contains letters. If it contains letters then I would like to run a method that tells them their input is incorrect.

Right now to do this I am having to write all the possibilities out e.g. if input.contains a || input.contains b , etc. and I can't write out every possibility and combination or use of the english alphabet. I just want to warn the user if they write letters because my program is a calculator.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Make use of the [String#matches()](https://www.tutorialspoint.com/java/java_string_matches.htm) method and a small [Regular Expression](https://www.vogella.com/tutorials/JavaRegularExpressions/article.html) (RegEx): `if(myString.matches("(?i)[a-z]+") { System.out.println("Only letters A-Z have been entered (letter case ignored)."); }`. Recursion is where you call Bob from within Bob. – DevilsHnd - 退職した Nov 08 '20 at 07:07

1 Answers1

0

You want to use Character.isLetter(). Something like :

if(Character.isLetter(input)) {
   //do something
} else {
   //do something else
}

Here's an example

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • Another question, if I have a method (e.g. Bob) and in this method I call the method itself i.e. I call Bob but with a different or new argument, is it correct to say this is a recursive method? I would put it in if statements too and only if some conditions are met.. the method would call itself – DeadlyDragonnn Nov 07 '20 at 18:12