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

Displaying each number of an integer in a sequence

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

Problem

One of the books I'm currently reading to learn C# asked me to write a method that takes an integer between 1 and 99999 and displays each number in that integer in a sequence, separating each digit by 2 spaces.

For example: the int 87564 would be written to the console as 8 7 5 6 4.

This is simple enough to do using the input as a string, converting it to a char array, or looping over the string with a foreach and printing out a formatted string for each character.

For fun though and mostly to challenge myself, I like to work out the problems as they are intended for someone just learning the concepts for the first time. The chapter was about methods and briefly introduced recursion. It's clearly the author's intent that you solve this using division and modulus operations to pick off each digit and then write them out.

So there really were limited options in terms of solving this with the material you have learned to this point in the book. You could pick off each digit and store it as it's own variable, then later write them out in order since you know the range of integers.

I decided to make the method more useful by really allowing any non-negative integer and my approach involved recursion. I'm not really experienced using recursion so I'd like to get some feedback on my implementation to see what I could have done better.

public class Program
{
    static void Main()
    {
        // Get User Input From Console
        // Validate and parse as int input
        DisplayDigits(input);
    }

    static void DisplayDigits(int value)
    {
        if (value < 10)
        {
            Console.Write("{0}  ", value);
            return;
        }
        DisplayDigits(value / 10);
        Console.Write("{0}  ", value % 10);
    }
}


Obviously, this is not meant to be production level code, I'm solving the problem in the textbook and then throwing the code away.
It appears to be working for all non-negative numbers I've tried. I even wrote an over

Solution

Your method is lying. Not only it's displaying the digits, performing every Console.Write operation that needs to happen, it's also performing the "digit-splitting" logic.

It's more work than what its name says. For a simple coding exercise it's without consequences, but in larger projects if this is a coding habit you have, it can mean much bigger problems.

Separation of Concerns

Let's see what needs to happen here.

  • Get a valid user input.



  • Determine what the digits are.



  • Come up with a string to output.



You haven't included how you're addressing the first concern, so I won't cover it here.

Ideally, the result of determining what the digits are should be just that: some IEnumerable that contains all the digits you want to display. I would make it so that the recursive function that contains the logic for that, works with an IList and adds each digit it finds (instead of outputting it directly to the console).

Then, when your recursive logic completes, you have an IEnumerable to iterate through. Right? Maybe not. string has methods, such as Join() that can make your "digit separator" string very explicit - if you're using .net 4+ you can use the Join(string, IEnumerable) overload:

var input = GetValidUserInput();
if (string.IsNullOrEmpty(input))
{
    return;
}

var digits = SeparateTheDigits(input);

var separator = "  ";
var result = string.Join(separator, digits);

Console.WriteLine(result);


As a result of separating the concerns, you now only write to the console once... and you haven't hard-coded your digit separator into a format string.

Code Snippets

var input = GetValidUserInput();
if (string.IsNullOrEmpty(input))
{
    return;
}

var digits = SeparateTheDigits(input);

var separator = "  ";
var result = string.Join(separator, digits);

Console.WriteLine(result);

Context

StackExchange Code Review Q#41174, answer score: 20

Revisions (0)

No revisions yet.