Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,690 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,313 people online right now. Registration is fast and FREE... Join Now!




How to catch NaN infinity etc errors

 
Reply to this topicStart new topic

How to catch NaN infinity etc errors, error handling

Ak-Emm
post 5 Sep, 2008 - 03:19 AM
Post #1


New D.I.C Head

*
Joined: 18 Aug, 2008
Posts: 22



Thanked 1 times
My Contributions


Example lets say i want to detect a NaN error
when detected a error dialog appears

taking this as an example
CODE

String s;
        float Number;
        double Ans;
        s=txtNumber.getText();
        Number=Integer.parseInt(s);
        Ans= Number*2;
       s=String.valueOf(Math.toRadians(Ans));
      txtProduct.setText(s);
      if ((Ans).equals("NaN"));
      if(true);
      JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


Can some1 tell me where i am wrong this works but for everything
including Nan and Infinity i only want to detect NaN or Infinity


now ive tried the second method isNaN and isInfinite
CODE

if((double).IsNaN(Ans)); or .IsInfinite(Ans)
if(true);  same as above and

even with catch
}catch (ArtithmeticException e) method the second and third dont give errors and they dont even detect NaN nor Infinite

your help on this one

thanks

*1lacca: inserted code tags, next time please use them like this: code.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 5 Sep, 2008 - 04:42 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Try changing this:

CODE
if ((Ans).equals("NaN"));
if(true);
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


To something like this:

CODE
if ((Ans).equals("NaN"))
if(true)
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


or this:

CODE
if ((Ans).equals("NaN")){
  if(true){
    JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);
  }
}


NOTE - Having a semi-colon after an if statement makes it think that the statement is complete and it just moves on, you code before was acting like so:

CODE
if ((Ans).equals("NaN")){
}
if(true){
}
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


So that nothing was happening in the if statements, and the JOptionPane was just aportion of th ecode that happened in a linear fashion without worrying about the if statements.

Hope that helps.

ALSO - In the future please post your code in the code tags, Thanks.
User is offlineProfile CardPM

Go to the top of the page

Ak-Emm
post 5 Sep, 2008 - 06:16 AM
Post #3


New D.I.C Head

*
Joined: 18 Aug, 2008
Posts: 22



Thanked 1 times
My Contributions


[quote name='BetaWar' date='5 Sep, 2008 - 05:42 AM' post='413212']
Try changing this:

CODE
if ((Ans).equals("NaN"));
if(true);
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


To something like this:

CODE
if ((Ans).equals("NaN"))
if(true)
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


or this:

CODE
if ((Ans).equals("NaN")){
  if(true){
    JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);
  }
}


NOTE - Having a semi-colon after an if statement makes it think that the statement is complete and it just moves on, you code before was acting like so:

CODE
if ((Ans).equals("NaN")){
}
if(true){
}
JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);


So that nothing was happening in the if statements, and the JOptionPane was just aportion of th ecode that happened in a linear fashion without worrying about the if statements.

Hope that helps.


Thanks for your support







it didnt make much of a difference it still returns true even though NaN or Infinity is not present


ive tried this

if (txtProduct.setText(s).isNaN(Ans)){
}
if(true){

dialog should appear

i get an error saing double or float or void cannot be differenced



ive tried private Boolean either returns it false or true

catching failed to detect too

"NaN" .isNaN i was told .isNaN is a more accurate method

This post has been edited by Ak-Emm: 5 Sep, 2008 - 06:23 AM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 5 Sep, 2008 - 02:16 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


This should work:

CODE
if ((Ans).equals("NaN")){
  if(true){
    JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);
  }
}


However, if the input is a single character it will have a numeric value (as Java auto translates characters to their numeric values when set to an int, etc.) What you have to do is also make sure that the number is not a character, which has to be done like so:

CODE
if(Ans<'a' || Ans>'z'){


and do that for capitals also.

HTH
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 6 Sep, 2008 - 07:52 AM
Post #5


D.I.C Addict

Group Icon
Joined: 6 Oct, 2006
Posts: 517



Thanked 3 times

Dream Kudos: 125
My Contributions


QUOTE

ive tried this

if (txtProduct.setText(s).isNaN(Ans)){
}
if(true){

dialog should appear



No it should not. your syntax is quite off.
The if statement is simply not used.

try this

CODE

if (txtProduct.setText(s).isNaN(Ans)){
    // alert("NaN detected");
    JOptionPane.showMessageDialog(null,"Try Again.","Inane warning",JOptionPane.ERROR_MESSAGE);

}else{
    // do other things
}
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:40AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month