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

Thread: application that determines and prints the number of odd, even, and zero digits

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

    Default application that determines and prints the number of odd, even, and zero digits

    my code will not read zero's when they are in the number. Please help
    import java.util.Scanner;
    public class oddevenzero {
    	public static void main (String[] args){
     
    	int numofOdd = 0, numofEven = 0, numofZero = 0, length, left = 0;
    		String num;
    	Scanner scan = new Scanner (System.in); //instantiate Scanner object
     
    	System.out.println ("This program will tell you how many even, odd, and zeros are in an integer: "); 
    	System.out.println("Enter a integer: "); 
    		num = scan.next(); 
    	System.out.println(""); 
     
    	length=num.length();
     
    	while (left < length){
     
    		num.charAt(left);
     
    			if (num.charAt(left) == 0)
    				numofZero = numofZero + 1;
    			else if (num.charAt(left)%2 == 0)
    				numofEven = numofEven + 1;
    			else
    				numofOdd = numofOdd + 1;
     
    			left = left +1; }
     
    	System.out.println ("Even Digits: " + numofEven);
    	System.out.println ("Odd Digits: " + numofOdd);
    	System.out.println ("Zero Digits: " + numofZero);
     
    	}
    }
    Last edited by JavaPF; October 19th, 2011 at 11:13 AM. Reason: Please use highlight tags. See my signature..


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: application that determines and prints the number of odd, even, and zero digits

    Have a look where you are testing if it is a zero. What does this method return?

    See:
    String (Java 2 Platform SE v1.4.2)
    "
    charAt(int index)
    Returns the character at the specified index.
    "

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: application that determines and prints the number of odd, even, and zero digits

    shouldn't the the sequence in the if statement of "(num.charAt(left) == 0)" take the user input and look at the value at which left was assigned (0) at the begginning of the program. then if that value is zero it adds to the numofZero? i do not understand why it does not.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: application that determines and prints the number of odd, even, and zero digits

    Yes, it will, but the code is testing if it is the number 0, rather than a character, or String containing "0", which is what the method is returning.

    When testing the content of a string, use String.equals(String); This will look at the content, rather than the object.

Similar Threads

  1. Separating Digits in a String
    By Staticity in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 06:39 AM
  2. Help with code that prints number of evens, odds, and 0's
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2011, 06:50 PM
  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. Remove last digits of an IP address.
    By Jonathan_C in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 15th, 2010, 12:55 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