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: Help with code that prints number of evens, odds, and 0's

  1. #1
    Banned
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with code that prints number of evens, odds, and 0's

    Okay, so my assignment was this: 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.
    What should happen is i enter a random number: 1234567890. it would then print out: There are 5 odd numbers. There are 4 even numbers. There are 1 zeroes.

    Here is my code i have written so far:
    public static void main (String[] args)
    {

    int zero = 0, odd = 0, even = 0, length, left = 0;

    String string;
    System.out.print ("Enter any positive integer: ");
    string = Keyboard.readString();

    length=string.length();

    while (left<length)
    {
    string.charAt(left);
    left++;
    if (string.charAt(left) == 0)
    zero++;
    else if (string.charAt(left) % 2 == 0)
    even++;
    else
    odd++;
    }

    System.out.println ("There are: "+ zero + " zeroes.");
    System.out.println ("There are: "+ even + " even numbers.");
    System.out.println ("There are: "+ odd + " odd numbers.");

    }
    }

    The program is compiling just fine, but when i run it i get this error:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range: 9
    at java.lang.String.charAt(String.java:558)
    at Three.main(Three.java:32)

    Please Help.
    Thanks


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Help with code that prints number of evens, odds, and 0's

    In your While loop, you increment your "left" integer too early. Therefore, when you check the character at left, it will be out of range. Just place your increment operator - "left++;" - at the end of the while loop.
    Simplicity calls for Complexity. Think about it.

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

    trogan234 (October 2nd, 2011)

  4. #3
    Banned
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code that prints number of evens, odds, and 0's

    Solved. Dude I love you.

Similar Threads

  1. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  2. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  3. nothing prints after runing code
    By darego in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2011, 11:14 AM
  4. [SOLVED] Help with code, Says number is NaN and i cant format it
    By NewGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2010, 08:28 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM