views:

275

answers:

6

I've 3 .jsp files. index.jsp and sqrtcalculator.jsp and error.jsp.

In index.jsp there's a text field and a button. In sqrtcalculator.jps there's the, well, calculator.

Now, when I leave the field empty and press the button, an expection is called, because the field is empty. And I can then reroute to an individual error.jsp site to display the errors, and exception stack like

<%= request.getAttribute("javax.servlet.error.request_uri") %>
<%= exception.getMessage() %>
<%= exception %>

etc.

Problem is, when I enter -5 in the textfield, I can't get an exception, because Math.sqrt(-5) doesn't return an error but "NaN".

How can I force it to be an exception? The idea is to reroute the user to an individual error.jsp and displaying the errors. But like I said Math.sqrt(-5) doesn't return errors or exceptions but "NaN" textstring.

I don't want something like this just FYI

<%if (NaN){
%>
<p>Nope, Square root from negative numbers is not defined!</p>
<%}
%>
+1  A: 

It's presumably returning NaN because square roots of negative numbers are indeed defined, they're just not real numbers. Is there any reason you can't do this?

if(Double.isNaN(answer))
    throw new ArithmeticException("Answer unreal");

That should tickle your exception handling code, but the source line might just be a few lines off. I've never written JSP, but that makes sense in Java.

Jed Smith
No real reason, I just thought maybe I can get it to force exceptions and display them like <%= exception.getMessage() %> or display the expcetion stack and so on.
NoCanDo
Well, you're making an exception and throwing it -- it will, unless you catch it.
Jed Smith
+1  A: 

How about:

if (x >= 0.0) {
    return Math.sqrt(x);
} else {
    throw new ArithmeticException("Square root from negative numbers is not defined!");
}

Note that ArithmeticException is a subclass of RuntimeException and therefore does not need to be declared in the throws clause of your function.

Alnitak
A: 

I'm not very good in jsp but afaik you should be able to use basic java code.. so i'd check my result for NaN and throw an exception

if (NaN == result)
  throw new Exception("NaN");

you'll have to adjust the if ;)

Dimitri
As noted above the equality operator does not work in the expected fashion with NaN. A NaN is never equal to anything, including a NaN.
Mark Thornton
A: 

I would ask why you're using scriptlets in JSPs.

If your app really is just three pages (index.jsp and sqrtcalculator.jsp and error.jsp), then maybe it's justifiable.

But my general recommendation would be to stay away from scriptlet code in JSPs and prefer JSTL. Have those calculations done in a server-side component, using a Model-2 MVC arrangement.

If your app really consists of only three pages, re-architecting it to use Model-2 MVC would not be difficult to do. You might find that it's far more extensible that way. If you're unfamiliar with Model-2 MVC you can read about it here. If you're familiar with it, this might be a self-contained problem that's small enough to let you see where it could provide some value in future projects.

Another thought would be to add complex numbers to your calculator, because the square root of negative numbers is indeed a valid concept. It just requires imaginary numbers. Maybe your calculator needs to be extended to maximize usefulness.

duffymo
I don't think the answers given here always have to be narrowly focused on the person who asks the question. If somebody doesn't appreciate that scriptlet code is 1998 style of doing things, or that complex numbers are indeed useful, it's worth having it pointed out. It's not about my preference, because I honestly don't care how this person decides to approach it. It's about educating someone about another possibility, that's all.
duffymo
Hey, sometimes it might be just the thing that someone needs to hear. There was nothing in the original question to suggest that the questioner even knew what a complex number was. And scriptlets? Please. By your own admission, you haven't written much Java or JSPs. Why are you being so tough on advice in an area that you don't have much expertise in?
duffymo
I tend not to be tough on things that I don't know well. If I have five people telling me that my approach was less than optimal, I might start questioning myself instead of worrying about being "wrong". In any case, we're all looking forward to taking instruction from you.
duffymo
Clearly, but that's not what I was saying. My point had nothing to do with iptables and everything to do with getting five people telling me the same thing. None of this is personal - you seem to be the one that is making it so. At no time did I use any words like "How on Earth is this helpful?", and I only see the word "ass" in your most recent answer. I'm handling your criticism just fine, thanks.
duffymo
If you re-read my "inference", you'll see that at no time did I question your use of iptables. It's a generic comment, not about your specific choice of technology.
duffymo
I know better than to fight a hopeless fight -- good luck to you.
Jed Smith
And to you as well.
duffymo
+2  A: 

Warning, the test for NaN in the previous answer is wrong (NaN is NOT equal to itself). In Java the better way to test for NaN is

if (Double.isNaN(answer)) {
  // throw exception
}

alternatively

if (answer != answer) {
}

While this second version will work, it is sure to puzzle those not aware of the curious behaviour of NaN.

Mark Thornton
You can add comments under answers -- I've edited mine. Thank you, been a while since I played with Java.
Jed Smith
NaN not being equal to itself is a property of IEEE 754 arithmetic and not specific to Java.
Mark Thornton
I can NOW add comments, previously my reputation was a mere 31 and thus did not permit comments to answers other than my own!
Mark Thornton
A: 

I can't seem to get it to work....

   if (d < 0){
        throw new Exception("Negative Numbers not possible!");
   }

d is a double read via d_string = request.getParameter("numberfromtextfield") and converted to double via double d = Double.parseDouble(d_string);.

This is what Eclipse is telling me: http://pastebin.ca/1691409

NoCanDo
Post the whole JSP - can't tell from the info you provided. Edit your original post if you can; don't keep creating new entries.
duffymo