promotion image of download ymail app

I have a question about Java - I have this expression:?

I have a question about Java - I have this expression:

if (!length.matches("[0-9]+") {

(comment line)


which displays my error message if the user enters anything except a positive number.

but what if I want it to allow EITHER a positive or negative number and give an error message if anything else is entered?

1 Answer

  • 3 months ago

    If you want to use regex, you'd perform some conditional regex on the first position, allowing for -. Of course, I don't know why you aren't just try-catching an attempt to cast the message to an int.

    E.g. -?[0-9]+

    • ...Show all comments
    • Mr.Persona
      Lv 5
      3 months agoReport

      It means zero or one of the preceding character. I forget if it has other uses, but anyhow, look at websites like regex101 to test regex patterns and learn what some mean.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.