LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java programming (https://www.linuxquestions.org/questions/programming-9/java-programming-3927/)

mAineAc 07-03-2001 02:48 PM

java programming
 
I am trying to get an applet to recognize if the input is a legal integer. I got it so that it will go to the program and do what it is supposed to do but it won't give a message if it is not a number. This is my code:

public void test() throws NumberFormatException {
if (testNumberField.getText() != null) {
PrimeTest();
}
else {
answer.setText("this is not a number!");
}
}


what am I doing wrong?

mAineAc

cubed 07-04-2001 09:10 AM

Hello mAineAc.

Firstly I'm probably newer to java than you but a few things ocurred to me when I saw this.

Here's some things to try:

Isn't an exception best dealt with using something like:

Code:

try {
      while (testNumberField.getText()!=null) {
        PrimeTest();
      }
} catch (NumberFormatException e) {
      System.out.println("This is not a number!"); // or answer.setText etc.
      }

Another thought - can you perhaps test for NaN ? (Don't know if that can be done but it's the first place I'd look)

This might have helped sorry if it hasn't.

mAineAc 07-04-2001 09:31 AM

thank you that is just what i was looking for. I am pretty new to java.

mAineAc

cubed 07-06-2001 09:42 AM

BTW, I just peeked at my CD reference (Java Power Reference from O'Reilly - highly recommended) and it does offer the following:
Code:

public static boolean java.lang.Double.isNaN(double v);

public static boolean java.lang.Float.isNaN(float v);

So in the brackets you put your number type and it'll return true or false if it's Not a Number.

Glad to be of help, mAineAc.


All times are GMT -5. The time now is 05:00 AM.