-4

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. (java)
using for loop

4
Contributors
7
Replies
37
Views
1 Year
Discussion Span
Last Post by AssertNull
1

You just copy/pasted your assignment without even a moment taken to explain what help you need. That's highly disrespectful to the many people who give their time to help others here.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

-1

Well, thanks for spending your precious time and yea i copy pasted it, it is my assignment but let me tell you i tried my best before posting it and never meant to cheat. I really tried many ways of doing it but needed help. But yes sorry i didn't mention what part i was stuck at.
thanks

0

Did you try google? https://www.google.com/search?q=Design+and+implement+an+application+that+determines+and+prints+the+number+of+odd%2C+even%2C+and+zero+digits+in+an+integer+value+read+from+the+keyboard.+%28j&ie=utf-8&oe=utf-8 finds working code from this old homework and you can rewrite the while to be a for loop.

You pasted the class assignment but need to show your work. Asking for the code is the second part of the assignment. The first part is design. Tell us your design.

0
import java.util.Scanner;
public class NumEvenOddZero {
    public static void main(String[] args){
        int odds=0;
        int evens=0;
        int zeros=0;
        Scanner kbd=new Scanner(System.in);
        System.out.println("Enter an integer: ");
        int input=kbd.nextInt();
        String s=""+input;
        int length=s.length();

        for(int i=0; i<length; i++){



            int x=input%10;
            input/=10;

            if(x==0){
                zeros++;
            }else if(x%2==0){
                evens++;
            }else{
                odds++;
            }


        }
        System.out.println("Number of odds: "+odds);
        System.out.println("Number of evens: "+evens);
        System.out.println("Number of zeros: "+zeros);
    }
}

this is what i did but when i run it, it doesn't work for zero!

0

input 0123 because when 0 is in the begining it doesnt work

1

If you need to count the leading zeroes, you can't use Scanner's nextInt by itself because it skips the leading zeroes. Either read the input in as a String and iterate through the String counting leading 0's, then converting the String to an Integer, or use Scanner's hasNext function along with Scanner's skip function in a while loop to go through the leading zeroes. Either way, you would be counting leading zeroes and incrementing your zeros variable.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.