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 3 of 3

Thread: Problem with my code (Fairly New to Java)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Problem with my code (Fairly New to Java)

    Hi java peoples,
    I just recently started programming java, and I having a problem with my program. I'm trying to determine the number of even, odd, and zero digits in a number inputted by the user. It counts even and odd digits fine, however, it determines every zero digit to be an even digit. For example, when I enter a number like 140, the program says that there are 2 evens, 1 odd, and 0 zeros, where it should be 1 even, 1 odd, and one zero. Any help would be appreciated.

    public class Project_6
    {
        public static void main (String[] args)
        {
            System.out.print ("Enter a number: ");
            String value = Keyboard.readString();
            int evens = 0;
            int odds = 0;
            int zeros = 0;
            int count = 0;
            int value_int;
     
            while (count < value.length())
            {
                value_int = value.charAt(count);
                if (value_int % 2 != 0)
                    odds++;
                else
                {
                    if (value_int == 0)
                        zeros++;
                    else
                        evens++;
                }
                count++;
            }
     
            System.out.println ("\nEven digits: " + evens + "\nOdd digits: " + odds + "\nZero digits: " + zeros);
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with my code (Fairly New to Java)

    If you want to really confuse yourself, add a statement like

    System.out.println( "The value is " + value_int );

    as the second line of your while statement.

    What? Why for 140 are the 'values' printed as 49, 52, and 48? Because those are the char values (or ASCII values) of the characters '1', '4', and '0'. So almost by coincidence, your code half works, because the ASCII values happen to be even for even characters and odd for odd characters. But the ASCII value is also even for the '0' character, so it gets counted as an even.

    When you add a test to catch zero, you'll also have to restructure your if/else statements some so that each is at the same level.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    headcase (September 24th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my code (Fairly New to Java)

    Thanks a lot. I didn't realize numeric characters get translated into their ASCII values.

Similar Threads

  1. Java code problem?
    By Scorks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2013, 06:13 PM
  2. New to Java having problem debugging my code.
    By OHJava in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 9th, 2013, 01:05 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Problem with fairly basic recursive method
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2011, 09:58 PM
  5. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM