我正在创建的 .jsp 页面有问题。对于那些担心的人来说,该网站是用于家庭作业的,但是,我试图超越要求的范围,并且不询问与评分有关的任何事情。这完全是为了我自己的利益。
言归正传:我正在从用户那里获得输入,执行 method="post" 并刷新页面,并且在理想情况下它可以工作(这是家庭作业。)但是,我正在尝试尝试/捕获在用户将字符串输入 int 字段的情况下阻止。
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>
<%
}
我尝试将“NumberFormatException”切换为“Exception”,但它返回了相同的错误代码。早些时候,我在 if() 语句之前有我的 try catch,它起作用了,但没有显示我想要的警报框。
该代码如下:
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.");
}
任何帮助表示赞赏,您的时间也是如此。非常感谢你!
编辑:对不起,意思是复制粘贴错误代码:
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "a"
测试输入是字符串“a”
EDIT2:备份/编辑/测试@Nambari 的建议
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.");
}
输出:理想电阻为:-90.0 Ohms
警告框说(“源电压必须是整数。请解决这个问题。”)
没有错误代码。
没有 out.println 说(“请确保您的输入正确”)。
最终编辑:
代码现在几乎可以按预期工作,但已经足够好了。
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.");
}
}
我知道很多行话,但这就是最终结果。不知何故,这一切正常。感谢所有帮助过的人!