Hi there guys,

I got this assignment where I need to set a session attribute. Although I'm having problems since I cant set attribute that is of type double? I got these hints but since I've worked with doubleValue() or downcasting, the hints didn't help me much..

Hints:
-Recall that session attributes are stored as objects and must be downcast.
-Because a Double object cannot be modified, use the Double method, doubleValue(),to return an intrinsic double value.

My code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.DecimalFormat;

public class SessionBank extends HttpServlet
{
	// Create Class Variable
	DecimalFormat myFormat = new DecimalFormat("$#,000.00");

	public void doGet(HttpServletRequest req,HttpServletResponse res)
						throws ServletException, IOException
	{
		doPost(req,res);
	}

	public void doPost(HttpServletRequest req,HttpServletResponse res)
						throws ServletException, IOException
	{
		//Makes instance variable a local variable
		double accBalance = 0.00;

		//Set MIME type of content returned to browser
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		//Starts outputting the HTML Form
		out.println("<html><head>" +
					"<title>Online Bank Simulator</title>" +
					"</head><body>" +
					"<hr color=\"#808000\">" +
					"<center><h1>Banking Simulation</h1></center>" +
					"<form method=\"POST\" action=\"../servlet/HTMLBank\">" +
					"<center>Amount: <input type=\"text\" name=\"Amount\" size=\"20\"></center>");

		//Decides which action to take
		String action = req.getParameter("act");

		if(action != null)
		{
			if(action.equals("Deposit"))
			{
				double amount;
				String strAmount = req.getParameter("Amount");
				amount = Double.parseDouble(strAmount);

				if(amount <= 0.00)
				{
					out.println("<h2>Error: The amount is either null or a minus</h2>");
				}
				else
				{
					accBalance = accBalance + amount;
					out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
				}
			}
			else if(action.equals("Withdraw"))
			{
				double amount;
				String strAmount = req.getParameter("Amount");
				amount = Double.parseDouble(strAmount);

				if(amount <= 0.00)
				{
					out.println("<h2>Error: The amount is either null or a minus</h2>");
				}
				else
				{
					accBalance = accBalance - amount;
					out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
				}
			}
			else if(action.equals("Balance"))
			{
				out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
			}
			else
			{
				out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
			}
		}
		else
		{
			out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
		}

		//Get the current session and set Attribute
		HttpSession session = req.getSession(true);
		session.setAttribute("accBalance",accBalance);

		//Outputs rest of HTML
		out.println("<table width=\"35%\" align=\"center\">" +
					"<tr><td width=\"33%\" align=\"center\">" +
					"<input type=\"submit\" name=\"act\" value=\"Deposit\">" +
					"</td>" +
					"<td width=\"33%\" align=\"center\">" +
					"<input type=\"submit\" name=\"act\" value=\"Withdraw\">" +
					"</td>" +
					"<td width=\"33%\" align=\"center\">" +
					"<input type=\"submit\" name=\"act\" value=\"Balance\">" +
					"</td></tr>" +
					"</table><br>" +
					"</form>" +
					"<hr color=\"#80800\">" +
					"</body></html>");

	}
}

Error at compile time:
setAttribute(java.lang.String,java.lang.Object) in javax.servlet.http.HttpSession cannot be applied to (java.lang.String,double) session.setAttribute("accBalance",accBalance);

Thanks for having a look at this!

Recommended Answers

All 4 Replies

The error says that you are trying to invoke the method setAttribute with parameters (String, double) whereas the expected types are (String, Object). Simply put, the compiler expects the second parameter to be of any reference type but definitely not a primitive. Convert the `double` primitive value to `Double` and it should work out fine. Look into the Javadoc of the `Double` class.

Thanks alot! Still don't really get the difference between Double and double.. should read up on it a bit.

Double is a wrapper class whereas `double` is a primitive numeric type. Read this and follow the links in the comments.

BTW, are you by any chance using pre Java 1.5 since for Java 5 and above, autoboxing should have taken care of this for you.

Yeah I'm using 1.4 hey. It's ridiculous that my college still teaches in this but that's South Africa for you. Can't wait to start coding in 1.6!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.