0

Wrote a method which takes in a String and checks to see the follow conditions:

  1. If String is "quit", it will terminate the program.
  2. If the String is any value other than an integer, it should return "Invalid input ".
  3. Any negative integers and also 0 should return "Invalid input".

However, when I passed in 10, it returned as "Invalid input"?

Please advise:

public static String validate(String input) {
    Pattern pattern = Pattern.compile(".*[^1-9].*");
    StringBuilder results = new StringBuilder();
    if (input.equals("quit")) {
        System.exit(1);
    } else if (!pattern.matcher(input).matches() == false) {
        results.append("Invalid input ");
        results.append("'");
        results.append(input);
        results.append("'");
    }
    return results.toString();
}

What's wrong with what I am doing?

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • Your regex matches any string that contains at least one character other than 1-9. This probably isn't at all what you want. – Kendall Frey Jun 24 '14 at 19:53
  • possible duplicate of [What is the regex for "Any positive integer, excluding 0"](http://stackoverflow.com/questions/7036324/what-is-the-regex-for-any-positive-integer-excluding-0) – Kendall Frey Jun 24 '14 at 19:58
  • `if (!pattern.matcher(input).matches() == false)` is the same as `if (pattern.matcher(input).matches())` – Pshemo Jun 24 '14 at 20:06

3 Answers3

2

You should write a pattern of what you expect instead of what you're not. As describe what you want is always simpler that describe the rest of it.

So you expect :

Pattern acceptPattern = Pattern.compile("[1-9][0-9]*");

You may consider make you conditional expression simpler and correct by not using both ! and == false at the same time:

Which will make :

if (!acceptPattern .matcher(input).matches()) {//Invalid input code}

or

if (acceptPattern .matcher(input).matches() == false) {//Invalid input code}

note :

You write if(!A == false) => if(A == true) => if(A) but which was the inverse

VinyJones
  • 1,014
  • 9
  • 9
1

It looks like you want to match one or more digits, where the first one is not a zero.

[1-9]\d*

If you want to force it to be the entire string, you can add anchors, like this:

^[1-9]\d*$
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
0

Your regex string doesn't allow for the presence of a zero (not just a lone zero).

That is, the string ".*[^1-9].*" is looking for "any number of characters, something that isn't 1-9, and any number of characters". When it finds the zero, it gives you your incorrect result.

Check out What is the regex for "Any positive integer, excluding 0" for how to change this.

Probably the most helpful solution on that page is the regex [0-9]*[1-9][0-9]* (for a valid integer). This allows for leading zeros and/or internal zeros, both of which could be present in a valid integer. In using Matcher#matches you also ensure that this regex matches the whole input, not just part of it (without the need to add in beginning and end anchors -- ^$).

Also, the line else if (!pattern.matcher(input).matches() == false) could be made a lot more clear.... maybe try else if (pattern.matcher(input).matches()) instead?

Community
  • 1
  • 1
ethanbustad
  • 1,594
  • 1
  • 16
  • 27