jsp的if判断问题

jsp的if判断问题

问题描述:

<body>
	<form action="text2.jsp" method="post">
		<table width="" border="1" align="center" cellspacing="1"
			cellpadding="1">
			<tr>
				<td>存款总额</td>
				<td><input name="Z">元</td>
			</tr>
			<tr>
				<td>存款期限</td>
				<td><select name="year">
						<option value="1">一年</option>
						<option value="2">二年</option>
						<option value="3">三年</option>
				</select></td>
			</tr>
			<tr>
				<td>币种</td>
				<td><input type="radio" name="BZ" value="RMB">人民币 <input
					type="radio" name="BZ" value="DOL">美元</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="提交"
					name="submit">
			</tr>
			<%
				double LL = 0;
				double LX = 0;
				int years = 0;
				double z = 10000;
				String BZ = request.getParameter("BZ");
				String year = request.getParameter("year");
				if (year != null) {
					years = Integer.parseInt(year);
				}
				String Z = request.getParameter("Z");
				if (Z != null) {
					z = Double.parseDouble(Z);
				}
				if (BZ != null) {
					if (BZ.equals("RMB")) {
						switch (years) {
						case 1:
							LL = 0.0175;
							break;
						case 2:
							LL = 0.0225;
							break;
						case 3:
							LL = 0.0275;
							break;
						}
					}
					if (BZ.equals("DOL")) {
						switch (years) {
						case 1:
							LL = 0.0035;
							break;
						case 2:
							LL = 0.0045;
							break;
						case 3:
							LL = 0.005;
							break;
						}
					}
				}
				%>
				<%
				LX = z * LL * years;
			%>
			<tr align="center">
				<td>年利率</td>
				<td><%=String.format("%.2f", LL * 100)%>%</td>
			</tr>
			<tr align="center">
				<td>利息</td>
				<td><%=String.format("%.2f", LX)%>元</td>
			</tr>
		</table>
	</form>
</body>

为什么当存款总额为空时提交会被if语句判断为符合,进行强制类型转换导致报错

Z==null是表示文本框不存在,Z.equals("")表示文本框里面没有输入数据。

if (Z != null) 

改为

if (Z != null && !Z.equals("")){