Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-01-2008, 09:15 PM   PM User | #1
Inquisit
New Coder

 
Join Date: Aug 2008
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Inquisit is an unknown quantity at this point
Question error detection how to detect NaN infinity etc in java

Question in Java

ive encountered a little challenge

ive tried to make my program detect an error

i.e such as NaN and then it displays a message dialog saying Syntax error

the problem is this works but the dialog keeps poping up when simple arithmetics are performed with proper answers NaN and Infinity
what i would like to achieve is just NaN or Infinity etc detected not everything

i.e
txtNumber1 ......N1
txtNumber2

sum=N1+N2

Answer=value of (sum);

i want the dialog to appear when Nan or Infinit appears in the Answer text
ive tried if but fails to detect it

i.e

if (sum).equals("NaN");
Messadialog appears saying syntax, math etc errors

||

if (sum).equals("Infinity");

Messadialog appears saying syntax, math etc errors user presses OK then refreshes the panel


this is where your expertise fits in

thanks
Inquisit is offline   Reply With Quote
Old 09-02-2008, 03:19 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I don't know of any numbers that can add up to NaN or Infinity, so I assume that this would be like if the user enters 'cat' or something like that right?
First, cast you're numbers and catch the exception tossed, then evaluate. I'd probably just throw on a check for it, but I have a feeling that Java may do that for you when you're trying to perform you're calculations. Its been a couple of years since I've done work in java (gotta get back into it though...), so I may be mistaken on that.
Code:
double d1, d2, result;
try
{
    d1 = Double.valueOf(txtNumber1);
    d2 = Double.valueOf(txtNumber2);
    result = d1 + d2;
    if (Double.isNaN(result))
    {
        throw new Exception('NaN result!');
    }
    if (Double.isInfinite(result))
    {
        throw new Exception('Result is Infinite');
    }
}
catch (Exception ex)
{
    // Do something, one of these didn't parse.
}
Another option is to let the method throw the exception, and catch it with you're main program. The advantage of this is that it forces the developer of the interface to gracefully handle the exceptions. Just add a throws Exception (or whatever exception type you want) as a part of you're method signature.

Ok, hope that helps!
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:22 AM.


Advertisement
Log in to turn off these ads.