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