0

I want to output the results of my program in a nice looking table. With table I mean the files listed among themselves then a tabulator and the score listed among themselves. How can I achieve that this works so? At the moment my program is able to print out ONE file at a time but ALL other scores among themselves. The result should look like this:

file1 score1
file2 score2
file3 score3

But at the moment it looks like this:

file1 score1 
score2 
score3
file2 score1
score2
score3

I think the 2nd foreach is wrong but how can I output one element of a list without for/foreach?

Thats my code:

List<string> _fileNameList = new List<string>();
List<double> _counterFleschScore = new List<double>();
string _fileNameList

foreach (string files in _fileNameList)
            {
                Console.Write(files + "\t\t\t\t");

                foreach (double listingFleschScore in _counterFleschScore)
                {
                    Console.Write("{0:N1}", listingFleschScore);
                    Console.WriteLine("");
                }
            }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user5462581
  • 159
  • 1
  • 3
  • 13

2 Answers2

1

In this case you should use a for loop. The problem is that you have a two, nested foreach loops.

Be sure that the score list is at least as big as file list, otherwise you may get an index out of range exception. For a completely safe solution you should add a few checks, which I've omitted for clarity.

List<string> _fileNameList = new List<string>();
List<double> _counterFleschScore = new List<double>();

for (var i=0;i<_fileNameList.Count;++i)
{
    Console.Write(_fileNameList[i] + "\t\t\t\t");
    Console.Write("{0:N1}", _counterFleschScore[i]);
    Console.WriteLine("");
}

An improved code would look something like this.

var _fileNameList = new List<string>();
var _counterFleschScore = new List<double>();

var count = Math.Min(_fileNameList.Count, _counterFleschScore.Count);
for (var i=0; i < count; ++i)
{
    Console.WriteLine(string.Format("{0}\t\t\t\t{1:N1}", _fileNameList[i], _counterFleschScore[i]));
}
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
0

Why going through all the trouble, just use: ConsoleTables, Nuget also available.

There is also Console Utilities again Nuget also available.

MaYaN
  • 6,683
  • 12
  • 57
  • 109
  • The post was having a technical code issue, which is not answered with this at all. This should rather be a comment IMO. – Zoltán Tamási Feb 12 '16 at 13:23
  • 1
    The OP said: `I want to output the results of my program in a nice looking table.` and my answer provides exactly that. As part of being a software developer we should learn not to re-invent the wheel :-) – MaYaN Feb 12 '16 at 13:25
  • Ok, you have the point with the title, but if someone not too advanced has troubles with nested loops then I don't think any Nuget package is appropriate. – Zoltán Tamási Feb 12 '16 at 13:27
  • 1
    That's why I also included the related project links including the source code and docs making it a no-brainer! – MaYaN Feb 12 '16 at 13:28