Note that there are some explanatory texts on larger screens.

plurals
  1. PONumberFormatException not being caught in try/catch
    primarykey
    data
    text
    <p>I am having a problem with a .jsp page I am creating. For those worried, the site was for homework, however, I am trying to go beyond what is required and am not asking anything related to grading. This is strictly for my own benefit.</p> <p>Down to business: I am getting an input from the user, performing a method="post" and refreshing the page, and in an ideal situation it works (that was the homework.) However, I am trying to make a try/catch block in the case where a user enters a string into an int field.</p> <pre><code>int Svolt = 0; double Amperes = 0.0; double LEDdrop = 0.0; if (request.getParameter("Svolt") != null) { try { Svolt = Integer.parseInt(request.getParameter("Svolt")); } catch ( NumberFormatException e ) { %&gt; &lt;script type ="text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% } </code></pre> <p>I've tried switching "NumberFormatException" to "Exception", but it returned the same error code. Earlier, I had my try catch come before the if() statement, and it worked, but didn't display the alert box I want.</p> <p>That code is below:</p> <pre><code>final double MA_TO_A = 0.001; int Svolt = 0; double Amperes = 0.0; double LEDdrop = 0.0; try { if (request.getParameter("Svolt") != null) { try { Svolt = Integer.parseInt(request.getParameter("Svolt")); } catch (Exception e) { %&gt; &lt;script type ="text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% } Amperes = Double.parseDouble(request.getParameter("Amperes")); LEDdrop = Double.parseDouble(request.getParameter("LEDdrop")); out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms."); } } catch (Exception e) { out.println("Please make sure your inputs are correct."); } </code></pre> <p>Any help is appreciated, and your time is, too. Thank you very much!</p> <p>EDIT: Sorry, meant to copy-paste the error code:</p> <pre><code>org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a" </code></pre> <p>Where the tested input was the string "a"</p> <p>EDIT2: Back-up/edit/test @Nambari's suggestion</p> <pre><code>final double MA_TO_A = 0.001; int Svolt = 0; double Amperes = 20.0; double LEDdrop = 1.8; try { if (request.getParameter("Svolt") != null) { try { Svolt = Integer.parseInt(request.getParameter("Svolt")); } catch (Exception e) { %&gt; &lt;script type ="text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% } out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms."); } } catch (Exception e) { out.println("Please make sure your inputs are correct."); } </code></pre> <p>Output: Ideal resistance is: -90.0 Ohms</p> <p>Alert box saying ("The source voltage must be an integer. Please fix this.")</p> <p>No error code.</p> <p>No out.println saying ("Please make sure your inputs correct").</p> <p>FINAL EDIT:</p> <p>Code now works almost as desired, but good enough.</p> <pre><code> final double MA_TO_A = 0.001; int Svolt; double Amperes; double LEDdrop; if (request.getParameter("Svolt") != null) { try { try { Svolt = Integer.parseInt(request.getParameter("Svolt")); } catch (Exception e) { %&gt; &lt;script type ="text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% Svolt = 0; } try { Amperes = Double.parseDouble(request.getParameter("Amperes")); } catch (Exception e) { %&gt; &lt;script type = "text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% Amperes = 0.0; } try { LEDdrop = Double.parseDouble(request.getParameter("LEDdrop")); } catch (Exception e) { %&gt; &lt;script type = "text/javascript"&gt; alert("The source voltage must be an integer. Please fix this."); &lt;/script&gt; &lt;% LEDdrop = 0.0; } out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms."); } catch (Exception e) { out.println("Please make sure your inputs are correct."); } } </code></pre> <p>Lots of jargon, I know, but that's the end result. Somehow, this has it all working. Thank you to all who helped!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload