423,849 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,849 IT Pros & Developers. It's quick & easy.

Even Numbers

Shinzon
P: 7
Hey guys and girls,

I am pretty well versed in perl, but am just learning Java and I was wondering if anyone could explain how I would ask for an input of only even numbers or maybe point me in the direction of some good reading.
Jun 8 '07 #1
Share this Question
Share on Google+
7 Replies


blazedaces
100+
P: 284
Hey guys and girls,

I am pretty well versed in perl, but am just learning Java and I was wondering if anyone could explain how I would ask for an input of only even numbers or maybe point me in the direction of some good reading.
What exactly do you mean? Asking for an input? When you print the text output say "only even numbers inputted: " or something...

I think you meant it would only accept even numbers, in which case you could simply test if the inputted number is even, if so continue, if not ask again or output an error.

What exactly are you unsure about? This is mostly simple math along with if statements, maybe you can use a while loop too... do you want to look at simple examples that use those? Check out sun's tutorials, they have quite a library chuck full of good examples and demos.

Good luck,
-blazed
Jun 8 '07 #2

10K+
P: 13,264
What exactly do you mean? Asking for an input? When you print the text output say "only even numbers inputted: " or something...

I think you meant it would only accept even numbers, in which case you could simply test if the inputted number is even, if so continue, if not ask again or output an error.

What exactly are you unsure about? This is mostly simple math along with if statements, maybe you can use a while loop too... do you want to look at simple examples that use those? Check out sun's tutorials, they have quite a library chuck full of good examples and demos.

Good luck,
-blazed
And while you are at it, don't forget to check out the Scanner class. The link to sun's docs and tutorials can be found here.
Jun 8 '07 #3

Shinzon
P: 7
Thank you both. I will look into it. Yeah I meant I have a input string of numbers:


Expand|Select|Wrap|Line Numbers
  1. System.out.print( "Please enter the employee hourly rate:\n" );
  2.        double hours = input.nextDouble(); 
  3.  
And want it to only accept even numbers such 2,4,6, etc
Jun 8 '07 #4

10K+
P: 13,264
Thank you both. I will look into it. Yeah I meant I have a input string of numbers:


Expand|Select|Wrap|Line Numbers
  1. System.out.print( "Please enter the employee hourly rate:\n" );
  2. double hours = input.nextDouble(); 
  3.  
And want it to only accept even numbers such 2,4,6, etc
An easy way is to read in any number and then test for even. An easier way is to contruct a regular expression for an even number and match for that ...
Jun 8 '07 #5

jeffbroodwar
100+
P: 118
Hi,

i hope this solution is not yet late..... why try using the modulus operator... i.e. modulus then 2.... if it returns 0 then it's even. if not then it must be odd.



Regards,
Jeff
Jun 8 '07 #6

Shinzon
P: 7
Never too late. I am still stuck. LOL I read through the material given but it seemed way over my head. I am still in the hello world stage of java programming and need some solid info on how i would only allow even numbers to be input in a string or double rather.
Jun 8 '07 #7

blazedaces
100+
P: 284
Never too late. I am still stuck. LOL I read through the material given but it seemed way over my head. I am still in the hello world stage of java programming and need some solid info on how i would only allow even numbers to be input in a string or double rather.
Dude, Even though r035198x right below my last post showed you the link to how to find the tutorials and classes, I'm going to help you out by pointing straight to the links for them:

Scanner Tutorial (learn how to input numbers as a string or a double, how convenient, those are the two examples they use):

http://java.sun.com/docs/books/tutor.../scanning.html

Scanner class:

http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html

Read through them and from now on, when you don't know how to do something like "read in number as string or double" search for it with a tutorial or something. There are plenty of them out there...

Good luck man,

-blazed
Jun 8 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.