Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Print odd, even and zero digits from an int

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Print odd, even and zero digits from an int

    Hello there. Here is my task which I am stuck on:
    "Design and implement an application that determines and prints the number of odd, even and zero digits in an integer value read from the keyboard."

    I have made variable pos the place at which the scanner is scanning a particular digit and made variable length equal the position of the 'farthest right' digit so that the scanning moves from right to left, checking each value of pos. It compiles and runs but the results I get are wrong. I would much appreciate any advice. Even if that means not using a for loop and starting fresh.
    Thanks

     
    //numbers.java
    import java.util.Scanner;
     
    public class numbers 
    {
      public static void main (String[] args)
      {
        int num, pos, odd=0, even=0, zero=0, length = 0;
     
        Scanner scan = new Scanner (System.in);
        System.out.println ("Please enter an integer: ");
        num = scan.nextInt();
     
        //.length() etc
     
     
        length = String.valueOf(num).trim().length();
     
          for (pos = length - 1; pos >= 0; pos--)
          {
            if (pos == 0)
              zero++;
            if (pos % 2 == 0)
              even++;
            if (pos % 2 != 0)
              odd++;
          }
     
       System.out.println("zeros: " + zero);
       System.out.println("evens: " + even);
       System.out.println("odds: " + odd);
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Print odd, even and zero digits from an int

    Why are you using the pos variable to determine whether the digit at that index is even or odd? That's just the index, not the digit itself.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Print odd, even and zero digits from an int

    Adding to that, have a look at the Code Conventions.
    Code Conventions for the Java Programming Language: Contents
    Class names start with an upper case letter. Also avoid class names that are already in the JRE.

  4. #4
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Print odd, even and zero digits from an int

    Quote Originally Posted by KevinWorkman View Post
    Why are you using the pos variable to determine whether the digit at that index is even or odd? That's just the index, not the digit itself.
    Ok so how can I pinpoint the integer at the position of pos and compare it to zero, even and odd ?

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Inspired
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Print odd, even and zero digits from an int

    Have a look at the String and Character classes they have some useful methods.

    For example you could use:

    char result = num.charAt(pos);

    to get the character at the given position and then use:

    int x = Character.getNumericValue(result);

    to convert it to an integer.

  6. The Following 2 Users Say Thank You to atheedom For This Useful Post:

    craigjlner (July 4th, 2013), ryan.boykin (August 1st, 2013)

Similar Threads

  1. Replies: 9
    Last Post: March 18th, 2013, 05:49 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  4. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM