Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 NEED HELP PLZ! java integer - real program
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a_vatamanescu




PostPosted: 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! Crying or Very sad
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: 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




PostPosted: 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.
code:
if (answer < 0 && answer > 0)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: