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.
if-statementwill only rundoSomething()if both methods returntrue. Which leads me to believe that the error is being thrown while running one of the methods.inputIsPositiveIntegerwill also accept empty string""which can later causeNumberFormatException. Consider using+instead of*. Also can you post more details about thrown exception? Stack trace would be helpful.