Akolopez
Akolopez

Reputation: 434

Java regex to match integer

I know that java short-circuits its boolean evaluations. So if (false && true) wouldn't reach the true condition since java already knows the first is false.

I'm having a problem though. I have to check if the input is a positive integer and is lesser than another integer.

The condition looks like this:

    if (inputIsPositiveInteger(input) && inputIsLessThanSomeNumber(input,someNumber)) {
      doSomething();
    }

    boolean inputIsPositiveInteger(String input) {
      String regex = "[0-9]*";
      return input.matches(regex);
    }

    boolean inputIsLessThanSomeNumber(String input, String someNumber) {
      return (Integer.parseInt(input) < Integer.parseInt(someNumber));
    }

This throws a NumberFormatException if my input isn't an integer since I'm parsing the input to an Integer in the second condition. I thought if the first condition was false it would just exit the if statement.

Can anyone shed light on this?

java.lang.NumberFormatException: For input string: "a"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at com.akolopez.servlets.ProductServlet.inputIsLessThanStock(ProductServlet.java:94)
    at com.akolopez.servlets.ProductServlet.validateInput(ProductServlet.java:78)
    at com.akolopez.servlets.ProductServlet.doPost(ProductServlet.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Edit: updated my post with the other methods. Edit2: I added a stack trace if it helps.

Upvotes: 0

Views: 10796

Answers (2)

orlandocr
orlandocr

Reputation: 323

Validate both arguments, input and someNumber:

void whatever(String input, String someNumber) {
    if (inputIsLessThanSomeNumber(input, someNumber)) {
        doSomething();
    }
}

boolean inputIsLessThanSomeNumber(String input, String someNumber) {
    if (inputIsPositiveInteger(input) && inputIsPositiveInteger(someNumber))
    {
        return (Integer.parseInt(input) < Integer.parseInt(someNumber));
    }

    return false;
}

boolean inputIsPositiveInteger(String input) {
    String regex = "[0-9]+";
    return input.matches(regex);
}

Upvotes: 2

ferzle
ferzle

Reputation: 103

If you are not certain your input is going to be in the correct format, you need to catch that exception. For instance, you could do the following:

try {
    int x = Integer.parseInt(input);
    int y = Integer.parseInt(someNumber);
    if(x>0 && x<y) {
        doSomething();
    }
} catch(NumberFormatException e) {
    // One of the inputs is not a number.
}

It also seems to make sense to do this way because you care about the inputs as integers, so convert them to integers and then do your comparisons.

Upvotes: 0

Related Questions