I have tried rozner’s code, but am still getting errors :'(, I have gone though loads of different tags to try and output it, but the closest I think I have got so far is this one:
To sum up what dc dalton said, you can’t cast from String to int like you can from Object to String.
If you want to convert a String to an int you use the code dc dalton posted:
what type variables are these two row.price * dvd.quantity (coming in that is)
First thing I noticed was that in that expression you have NOT converted them into the proper data types (at least not in the code we can see) where is that happening?
Also have you tested them FIRST (before the math) to make sure you are getting the type data you are expecting?
Always test the incoming data when you are having problems like this … one little miss-spelling of a variable’s name OR something like a $ in a variable can blow you up. (not that that is the problem here but again without seeing the rest of the code we are just guessing)
Reason I ask is because it looks like whatever’s coming out of that parse double isnt right!
If you look at the API:
static double parseDouble(String s) expects a String to be passed to it NOT a double!
If youre already converting those two variables into numbers you already have a double NOT a String!
row.price is a float value (eg 14.99) coming from a MySQL database and dvd.quantity is just an number (eg 1) in the session stored using Java Bean. (Its a DVD online store for my uni java assignment)
I have just displayed total, by just typing ${ total } and its output is 14.989999771118164
It outputs fine in JSP, but I cannot get the vaule into pure java code part eg <% %>. Any ideas how to get it working?
Double.parseDouble DOES NOT ACCEPT a number … it accepts a String!
HERE … right out of the API:
parseDouble
public static double parseDouble(String s) throws NumberFormatException
Returns a new double initialized to the value represented by the specified [B]String[/B], as performed by the valueOf method of class Double.
Parameters:
s - the [B]string[/B] to be parsed.
Returns:
the double value represented by the [B]string argument.[/B]
Throws:
NumberFormatException - if the string does not contain a parsable double.
Since:
1.2
See Also:
valueOf(String)
This is ONE REASON why I HATE JSPS … if it was a servlet the compiler would have SCREAMED bloody murder about it!
public void testNum() {
double num = 12.34;
int quantity = 2;
/* THIS WORKS results are a double */
System.out.println(num * quantity);
/* This will NOT work because the parse double ONLY accepts a String */
System.out.println(Double.parseDouble(num * quantity));
/* IF you really have to parse a double BACK into a double (god knows why) */
System.out.println(Double.parseDouble(""+num * quantity));
}
};
Here is the exact error message from the compiler:
NumTest.java:11: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)
This is ONE REASON why I HATE JSPS … if it was a servlet the compiler would have SCREAMED bloody murder about it!
This is probably the most important concept to grasp when working with JSP & Servlets. I went through the same thing trying to do calculations and processing, like connecting directly to JDBC, right in JSPs, very bad idea. This should be done in a servlet.
Of course this was all learning on my own so I was playing around with JSP & Servlets and it seemed natural coming from a PHP background, but as dc_dalton said, this type of work should be done in a Servlet. It makes life much easier. The compiler will generally tell you exactly what’s wrong where as JSP will give you a JasperException usually followed by a NullPointerException. Good luck figuring out where your problem is.