Printing out array as a table c#

Viewed 4569

I am have trouble getting my program to print out an array of Employee class objects neat and orderly. I will proved my code, could someone please let me know how I would structure my write statement to make this happen.

What the output actually is:

Bruce Wayne, 123456, 25.88, 35.50
Clark Kent 232344 25.88 38.75
Diana Prince 657659 27.62 30.25
Hal Jordan 989431 23.14 44.25
Barry Allan 342562 25.12 25.50
Arthur Curry 565603 21.09 23.75
John Jones 812984 18.99 32.75
Dinah Lance 342988 18.99 34.50
Oliver Queen 340236 17.45 41.25
Ray Palmer 120985 24.75 40.00
Ronald Raymond 239824 16.43 35.00
Carter Hall 657123 19.34 42.75
Shayera Hol 761742 16.73 38.50

What the output should be:

Bruce Wayne    123456 25.88 35.50
Clark Kent     232344 25.88 38.75
Diana Prince   657659 27.62 30.25
Hal Jordan     989431 23.14 44.25
Barry Allan    342562 25.12 25.50
Arthur Curry   565603 21.09 23.75
John Jones     812984 18.99 32.75
Dinah Lance    342988 18.99 34.50
Oliver Queen   340236 17.45 41.25
Ray Palmer     120985 24.75 40.00
Ronald Raymond 239824 16.43 35.00
Carter Hall    657123 19.34 42.75
Shayera Hol    761742 16.73 38.50

// This is from the Employee class.
public void PrintEmployee()
{
    Console.WriteLine(name + " " + number + " " + rate + " " + hours + " 
    " + gross);
}

// This is from main program.
for (int i = 0; i < Employees.Length; i++)
{
    if (Employees[i] == null)
    {
        break;
    }
    Employees[i].PrintEmployee();
 }

If you need more code please let me know. Also if anyone knows a nicer way of printing the table please let me know. All help is greatly appreciated.

3 Answers

Have a look at this post here: Format Strings in Console.WriteLine method

Padding will definitely be your answer. Depending on your needs, you can pad them at the Employee Class level on the getter in your properties.

Or do something like this:

var name = "Ronald Raymond";
var number = "239824";
var rate = "16.43";
var hours = "35.00";
var gross = "1256";

var formatted = string.Format("{0,-15}", name) +
    string.Format("{0,-7}", number) +
    string.Format("{0,-7}", rate) +
    string.Format("{0,-7}", hours) +
    string.Format("{0,-7}", gross);

Console.WriteLine(formatted);

If you look into the String.PadRight() method, you will see that it pads a string with whitespace characters until it is a specified width. We can do this with the name field, choosing some reasonable maximum name length (perhaps 25?) to size the column.

Also, instead of a PrintEmployee method, you might consider overwriting the ToString() method on your Employee class, so the default of employee.ToString() will look the way you want. For example:

public class Employee
{
    public string Name { get; set; }
    public int Number { get; set; }
    public double Rate { get; set; }
    public double Hours { get; set; }

    public override string ToString()
    {
        return $"{Name.PadRight(20)} {Number} {Rate:00.00} {Hours:00.00}";
    }
}

Now that we have the names displayed in a 20-character column (and the doubles are displayed with proper decimal places), things should line up well:

static void Main()
{
    var employees = new List<Employee>
    {
        new Employee {Name = "Bruce Wayne", Number = 123456, Rate = 25.88, Hours = 35.5},
        new Employee {Name = "Clark Kent", Number = 232344, Rate = 25.88, Hours = 38.75},
        new Employee {Name = "Diana Prince", Number = 657659, Rate = 27.62, Hours = 30.25},
        new Employee {Name = "Hal Jordan", Number = 989431, Rate = 23.14, Hours = 44.25},
        new Employee {Name = "Barry Allan", Number = 342562, Rate = 25.12, Hours = 25.50},
        new Employee {Name = "Arthur Curry", Number = 565603, Rate = 21.09, Hours = 23.75},
        new Employee {Name = "John Jones", Number = 812984, Rate = 18.99, Hours = 32.75},
        new Employee {Name = "Dinah Lance", Number = 342988, Rate = 18.99, Hours = 34.50},
        new Employee {Name = "Oliver Queen", Number = 340236, Rate = 17.45, Hours = 41.25},
        new Employee {Name = "Ray Palmer", Number = 120985, Rate = 24.75, Hours = 40.00},
        new Employee {Name = "Ronald Raymond", Number = 239824, Rate = 16.43, Hours = 35.00},
        new Employee {Name = "Carter Hall", Number = 657123, Rate = 19.34, Hours = 42.75},
        new Employee {Name = "Shayera Hol", Number = 761742, Rate = 16.73, Hours = 38.50},
    };

    foreach (var employee in employees)
    {
        Console.WriteLine(employee);
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Output

enter image description here

For the fun of programming, you could also do this:

String FixStringLength(String input, int length) {
   Char[] temp = new Char[length];
   for(int i = 0; i < length; i++){
      if(i < input.Length){
         temp[i] = input[i];
      }
      else{
         temp[i] = ' ';
      }
   }
   return new String(temp);        
}

And then

Console.WriteLine(FixStringLength(name, 30) + number); 
Related