HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Checking digits in a number

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
digitscheckingnumber

Problem

I'm working on an exercise that counts the number of even digits, odd digits, or zeros in a user-input integer (I made it a long so the user could put in more numbers). However I have a bunch of repetitive code when I'm building my output, especially about the pluralization of "[s]". Really, the entire thing feels redundant. How can I factor or simplify the code to be cleaner and most efficient? This isn't homework or anything, but I'm trying to do stuff out of the book to get more practice.

```
/****
* Program Name: Programming Project 4.5: Digit Checks in a Number
* Author: Terry Weiss
* Date Written: September 26, 2015
* Program Description:
* 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.
****/

import java.util.Scanner;

public class PP4_5
{

public static void main( String[] args )
{
long number;
boolean validInput = false;
String numStr;
Scanner input = new Scanner(System.in);

do
{
System.out.print("Please enter an integer: ");
numStr = input.next();

try {
number = Long.parseLong(numStr);
validInput = true;
} catch (NumberFormatException e) {
System.out.print("That's not an integer. ");
number = 0;
}
} while (!validInput);

// Break each digit up by dividing by powers of 10.
int evens = 0, odds = 0, zeros = 0;
for (long temp = number; temp > 0; temp /= 10)
{
int digit = (int)(temp % 10);

if (digit == 0)
{
zeros++;
System.out.println(digit + " is a zero

Solution

Really, the entire thing feels redundant

Well, it all comes down to the choice of programming language. With Java you chose predictability and regularity not power nor shortness. But I can still suggest improvements over this.

Your code does 3 things:

  • Take an integer input



-
Count evens, odds and zeros in its digits

2.1 Requires taking the digits of a number

-
Output this result nicely

3.1 Requires pluralization handling

So you should write three big methods and two smaller helper ones. This will not reduce size but will really increase readibility.

I started the re-factoring by extracting the input method, to show you what I mean in practice:

public class num
{
    public static long integerInput(String prompt) {
        long number;
        boolean validInput = false;
        Scanner input = new Scanner(System.in);

        do
        {
            System.out.print("Please enter an integer:  ");
            String numStr = input.next();

            try {
                number = Long.parseLong(numStr);
                validInput = true;
            } catch (NumberFormatException e) {
                System.out.print("That's not an integer. ");
                number = 0;
            }
        } while (!validInput);
        return number;

    }

    public static void main( String[] args )
    {

        long number = integerInput("The number? ");
        // Break each digit up by dividing by powers of 10.
        int evens = 0, odds = 0, zeros = 0;
        for (long temp = number; temp > 0; temp /= 10)
        {
            int digit = (int)(temp % 10);

            if (digit == 0)
            {
                zeros++;
                System.out.println(digit + " is a zero digit.");
            }
            else if (digit % 2 == 0)
            {
                evens++;
                System.out.println(digit + " is an even digit.");
            }
            else
            {
                odds++;
                System.out.println(digit + " is an odd digit.");
            }
        }

        String evenStr = " even " + ((evens == 1) ? "digit" : "digits");
        String oddStr = " odd " + ((odds == 1) ? "digit" : "digits");
        String zeroStr = (zeros == 1) ? " zero" : " zeros";

        System.out.println(number + " has " + evens + evenStr + ", " + odds + oddStr + ", and "
                + zeros + zeroStr + ".");
    }

}


The remaining 2 / 3 are up to you, good luck :)

Code Snippets

public class num
{
    public static long integerInput(String prompt) {
        long number;
        boolean validInput = false;
        Scanner input = new Scanner(System.in);

        do
        {
            System.out.print("Please enter an integer:  ");
            String numStr = input.next();

            try {
                number = Long.parseLong(numStr);
                validInput = true;
            } catch (NumberFormatException e) {
                System.out.print("That's not an integer. ");
                number = 0;
            }
        } while (!validInput);
        return number;

    }

    public static void main( String[] args )
    {

        long number = integerInput("The number? ");
        // Break each digit up by dividing by powers of 10.
        int evens = 0, odds = 0, zeros = 0;
        for (long temp = number; temp > 0; temp /= 10)
        {
            int digit = (int)(temp % 10);

            if (digit == 0)
            {
                zeros++;
                System.out.println(digit + " is a zero digit.");
            }
            else if (digit % 2 == 0)
            {
                evens++;
                System.out.println(digit + " is an even digit.");
            }
            else
            {
                odds++;
                System.out.println(digit + " is an odd digit.");
            }
        }

        String evenStr = " even " + ((evens == 1) ? "digit" : "digits");
        String oddStr = " odd " + ((odds == 1) ? "digit" : "digits");
        String zeroStr = (zeros == 1) ? " zero" : " zeros";

        System.out.println(number + " has " + evens + evenStr + ", " + odds + oddStr + ", and "
                + zeros + zeroStr + ".");
    }

}

Context

StackExchange Code Review Q#105779, answer score: 5

Revisions (0)

No revisions yet.