Even Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shinzon
    New Member
    • Jun 2007
    • 7

    Even Numbers

    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.
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by Shinzon
    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

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by blazedaces
      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.

      Comment

      • Shinzon
        New Member
        • Jun 2007
        • 7

        #4
        Thank you both. I will look into it. Yeah I meant I have a input string of numbers:


        Code:
        System.out.print( "Please enter the employee hourly rate:\n" );
               double hours = input.nextDouble();
        And want it to only accept even numbers such 2,4,6, etc

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Shinzon
          Thank you both. I will look into it. Yeah I meant I have a input string of numbers:


          Code:
          System.out.print( "Please enter the employee hourly rate:\n" );
          double hours = input.nextDouble();
          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 ...

          Comment

          • jeffbroodwar
            New Member
            • Oct 2006
            • 118

            #6
            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

            Comment

            • Shinzon
              New Member
              • Jun 2007
              • 7

              #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.

              Comment

              • blazedaces
                Contributor
                • May 2007
                • 284

                #8
                Originally posted by Shinzon
                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):

                This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment


                Scanner class:



                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

                Comment

                Working...