I ran into problem for my school assignment and it is to display whatever in a meal.csv file, here is a code we are starting with. The array string of itemDetails is where the data is store then passed into array ReadLines, then in Main, it is passed to mealContent, but when I tried Console.WriteLine(mealContent); it only prompted System.String[]. So how do I at least display it out in the command prompt. Thanks for the help guys~

Code/meal.csv

lunch,bento box b - sashimi,box combo,$9.59
dinner,vegetable sushi,6 rolls,$3.50
dinner,tuna roll,3 rolls,$4.50
dinner,roe, 2 rolls,$3.95
lunch,bento box a - chicken teriyaki,box combo,$8.59

FileIOManager/FileLocation.cs

using System;

namespace FileIOManager
{
    static class FileLocation
    {
        public const string INPUT_FILE = "../../Data/meals.csv";
    }
}

FileIOManager/FileReader.cs

using System;
using System.IO;

namespace FileIOManager
{
    static public class FileReader
    {
        private static int GetLineCount()
        {
            StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);
            int counter = 0;

            while (!sr.EndOfStream)
            {
                counter++;
                sr.ReadLine();
            }
            sr.Close();
            return counter;
        }

        public static string[] ReadLines()
        {
            int totalItems = GetLineCount();
            string[] itemDetails = new string[totalItems];
            StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);

            string itemDetail;
            int counter = 0;

            while (!sr.EndOfStream)
            {
                itemDetail = sr.ReadLine();
                if (itemDetail.Trim() != "")
                    itemDetails[counter++] = itemDetail;
            }
            sr.Close();
            return itemDetails;
        }
    }
}

MealMenu/Program.cs

using System;
using FileIOManager;
namespace Assignment3_Starter
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] mealContent = FileReader.ReadLines();
            Console.ReadLine();
        }
    }
}

You need to iterate over your array and write out each one individually. You can do this using a while, for or foreach loop. Have you been shown how to use any of these loops?

yes, we have, actually i got it working. But our instructor wants us to get it like this. Price = Cost * 1.8 then print it out again to look like this so im having trouble this part

* Lunch Items *
$15.46 bento box a - chicken teriyaki, box combo
$17.26 bento box b – sashimi, box combo

* Dinner Items *
$7.11 roe, 2 rolls
$8.10 tuna roll, 3 rolls
$6.30 vegetable sushi, 6 rolls

string[] mealContent = FileReader.ReadLines();
            foreach (string m in mealContent)
            {
                Console.WriteLine(m);
            }
            Console.ReadLine();

Why so much code?
Try this to get started

static void Main(string[] args)
        {
            string[] meals = System.IO.File.ReadAllLines("meals.csv");
            foreach (string meal in meals)
                Console.WriteLine(meal);
            Console.ReadKey();
        }

if I do out side of main()

static String First;

then inside of main

foreach (string meal in meals)
            {
                First = meal;               
            }
Console.WriteLine(First);

it will print out the first line of the meal.csv
but how to I do the 2 line and the 3rd and so on
then afterwards i can use Split() to separate the , thingy

Edited 3 Years Ago by Dani: Formatting fixed

Something like this: Using a class to handle the information. You can then do all kinds of things inside that class such as add a method to apply your Cost = 1.8 * Price

class Program
    {
        static void Main(string[] args)
        {
            string[] meals = System.IO.File.ReadAllLines("meals.csv");
            List<MealContent> menu = new List<MealContent>();
            List<string> mealTimes = new List<string>();
            foreach(string meal in meals)
            {
                if( !string.IsNullOrEmpty(meal) )
                    menu.Add(new MealContent(meal));
            }

            foreach (MealContent content in menu)
                if (mealTimes.IndexOf(content.MealTime) == -1)
                    mealTimes.Add(content.MealTime);

            mealTimes.Sort();
            foreach(string str in mealTimes)
            {
                Console.WriteLine("* " + str + " *");
                foreach (MealContent content in menu)
                    if (content.MealTime.Equals(str))
                        Console.WriteLine(string.Format("${0} {1}, {2}", content.Price, content.Entre, content.UnitOfIssue));
                Console.WriteLine();
            }

            Console.ReadKey();
        }

        class MealContent
        {
            public string MealTime;
            public string Entre;
            public string UnitOfIssue;
            public double Price = 0.0d;
            public MealContent(string csvData)
            {
                string[] parts = csvData.Split(',');
                MealTime = parts[0];
                Entre = parts[1];
                UnitOfIssue = parts[2];
                parts[3] = parts[3].Replace('$', ' ').Trim();
                Price = double.Parse(parts[3]);
            }
        }
    }

Edited 6 Years Ago by JerryShaw: n/a

interesting.. btw any chance without list, is it possible?

Sure, you can do it without using List<>, just use an array of MealContent. You can even do it without MealContent. There is enough info within the example for you to figure it out.

This article has been dead for over six months. Start a new discussion instead.