-1

I need to put the data from this table into an array and then make the array print as a formatted table in the console. Here's the table that I got my data from http://puu.sh/oqV8f/7d982f2665.jpg ; I just need to make the array output rows and columns instead of a list. I have this so far:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zumba1
{
    class Zumba
    {
        static void Main(string[] args)
        { //Recreated the data in the table for the zumba section, added each row, and each column.
            string[,] schedule = new string [8, 6] { { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                 {"Monday", "12", "10", "17", "22", "244",   },
                                 {"Tuesday", "11", "13", "17", "22", "252",},
                                 {"Wednesday", "12", "10", "22", "22", "264",},
                                 {"Thursday", "9", "14", "17", "22", "248",},
                                 {"Friday", "12", "10", "21", "12", "220",},
                                 {"Saturday", "12", "10", "5", "10", "148"},
                                 {" ", " ", " ", " ", " ","1376",}};
            foreach (string i in schedule)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadKey();
        }
    }
}

Any Ideas?

  • Where are you stuck? – mbeckish Apr 21 '16 at 20:41
  • you have a 2D array and you want to make it into a table? what kind of table? html table? database table? – dfdsfdsfsdf Apr 21 '16 at 20:41
  • Possible duplicate of [Format a string into columns](http://stackoverflow.com/questions/2978311/format-a-string-into-columns) – Adam V Apr 21 '16 at 20:42
  • Possible duplicate of [How can I align text in columns using Console.WriteLine?](http://stackoverflow.com/questions/4449021/how-can-i-align-text-in-columns-using-console-writeline) – mbeckish Apr 21 '16 at 20:43
  • What's the issue? Converting a db table to a 2D array of string? Or align a 2D array in the console? – Siyavash Hamdi Apr 21 '16 at 20:49

3 Answers3

1

Foreach on a [,] array gives you all elements as a list, as you noticed. In this case you need to output as follow:

for (int x0 = 0; x0 < schedule.GetLength(0); x0++)
{
    for (int x1 = 0; x1 < schedule.GetLength(1); x1++)
    {
        Console.Write("{0}\t", schedule[x0, x1]);
    }
    Console.WriteLine();
}
Console.ReadKey();

If you want to use foreach for any reason, you can also declare your table as [][] array. But in both ways you have to create 2 loops:

string[][] schedule = new string[][] {
                                    new string[] { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                    new string[] {"Monday", "12", "10", "17", "22", "244",   },
                                    new string[] {"Tuesday", "11", "13", "17", "22", "252",},
                                    new string[] {"Wednesday", "12", "10", "22", "22", "264",},
                                    new string[] {"Thursday", "9", "14", "17", "22", "248",},
                                    new string[] {"Friday", "12", "10", "21", "12", "220",},
                                    new string[] {"Saturday", "12", "10", "5", "10", "148"},
                                    new string[] {" ", " ", " ", " ", " ","1376",}
        };
foreach (string[] line in schedule)
{
    foreach (string i in line)
        Console.Write("{0}\t", i);
    Console.WriteLine();
}
Matt
  • 4,612
  • 1
  • 24
  • 44
0

If you use a monospaced font in the console you can adjust where each thing displays by putting more spaces if necessary

For example for the members corresponding to the the 1st and 2nd row and 1st of second column this would be the thing to calculate:

Largest word is Wednesday that is 9 letters, on first row first column I should put 9 espaces as there would be a blank. Then you might put four spaces as a separator between columns, then for the second column you calculate that 1:00 is the largest string so for 12 you would add 2 extra spaces, and so on.

Using a tab instead of some spaces is also likely to work but if the table ends up having some string that is much larger than one in another column it won't work.

Hope it helps.

user2638180
  • 1,013
  • 16
  • 37
0

Got it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zumba1
{
    class Zumba
    {
        static void Main(string[] args)
        { //Recreated the data in the table for the zumba section, added each row, and each column. Worked on formatting.
            string[,] schedule = new string[8, 6] { { "\t\t1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                 {"Monday", "\t12", "10", "17", "22", "$244",   },
                                 {"Tuesday", "\t11", "13", "17", "22", "$252",},
                                 {"Wednesday", "12", "10", "22", "22", "$264",},
                                 {"Thursday", "9", "14", "17", "22", "$248",},
                                 {"Friday", "\t12", "10", "21", "12", "$220",},
                                 {"Saturday", "12", "10", "5", "10", "$148"},
                                 {" ", " ", " ", " ", " ","\t$1376",}};
            //Nested for loops to print in a table-style format.
            for (int i = 0; i < schedule.GetLength(0); i++)

            {
                for (int j = 0; j < schedule.GetLength(1); j++)
                {
                    Console.Write(schedule[i, j] + "\t");
                }
                {
                    Console.WriteLine(i.ToString());
                }

            }
     Console.ReadLine();
            }
        }
    }
}