
-----------------------------------
a_vatamanescu
Wed Nov 03, 2004 8:18 pm

NEED HELP PLZ! java integer - real program
-----------------------------------
Hey eevryone. Im stuck at a program. The user inputs a number from which i have to make the program tell him/her if its either an integer or real. How would i do that?

i tried doin this: 
if (answer < 0 && answer > 0)
             c.drawString ("Your number: " + answer + ".. is an integer", 250, 200);
        else
            c.drawString ("Your number: " + answer + ".. is real", 250, 200);

BUT IT DOESNT WORK! if i input a decimal it gives me an error!. plz help me, i really need it!   :cry:

-----------------------------------
zylum
Wed Nov 03, 2004 9:06 pm


-----------------------------------
wheres the part of the code where you get the input? 

once you get the input then you could do:

if (Math.floor(number) == number) {
    System.out.println("this is an integer");
else {
    System.out.println("this is not an integer");
}

-----------------------------------
foobar247
Thu Nov 04, 2004 11:08 pm


-----------------------------------
you could try making use of the string parsing functions in Integer class
so here you can test if some string can be parsed to int.


try{
                Integer.parseInt(inputString);
                System.out.println("is an integer");
}
catch(NumberFormatException e){
                System.out.println("not an int");
}


i'm not sure what was the desired effect of your code. as nothing would test true.if (answer < 0 && answer > 0) 
