Posted: Wed Nov 03, 2004 8:18 pm Post subject: 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!
Sponsor Sponsor
zylum
Posted: Wed Nov 03, 2004 9:06 pm Post subject: (No subject)
wheres the part of the code where you get the input?
once you get the input then you could do:
code:
if (Math.floor(number) == number) {
System.out.println("this is an integer");
else {
System.out.println("this is not an integer");
}
foobar247
Posted: Thu Nov 04, 2004 11:08 pm Post subject: (No subject)
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.
code:
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.