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.
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.
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 ) {
%>
<script type ="text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
}
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.
That code is below:
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)
{
%>
<script type ="text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
}
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.");
}
Any help is appreciated, and your time is, too. Thank you very much!
EDIT:
Sorry, meant to copy-paste the error code:
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a"
Where the tested input was the string “a”
EDIT2:
Back-up/edit/test @Nambari’s suggestion
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)
{
%>
<script type ="text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
}
out.println("Ideal resistance is: " + ((Svolt - LEDdrop) / (Amperes * MA_TO_A)) + " Ohms.");
}
} catch (Exception e) {
out.println("Please make sure your inputs are correct.");
}
Output:
Ideal resistance is: -90.0 Ohms
Alert box saying (“The source voltage must be an integer. Please fix this.”)
No error code.
No out.println saying (“Please make sure your inputs correct”).
FINAL EDIT:
Code now works almost as desired, but good enough.
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)
{
%>
<script type ="text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
Svolt = 0;
}
try
{
Amperes = Double.parseDouble(request.getParameter("Amperes"));
} catch (Exception e)
{
%>
<script type = "text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
Amperes = 0.0;
}
try
{
LEDdrop = Double.parseDouble(request.getParameter("LEDdrop"));
} catch (Exception e)
{
%>
<script type = "text/javascript">
alert("The source voltage must be an integer. Please fix this.");
</script>
<%
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.");
}
}
Lots of jargon, I know, but that’s the end result. Somehow, this has it all working. Thank you to all who helped!
This is posted as answer based on my comments:
I would suggest remove everything except the Svolt related code on your form and try. I think something else messing up than this filed. Take a backup of existing code. Remove everything except Svolt and see, we can nail down the issue.
Make sure try/catch blocks are properly synched with code flow as suggested in above comments.