I am trying to create a program that reads from a text file one item at a time until it has read all the items. Each time it reads an item to the screen, the user enters 1, 2, or 3. Depending on what they enter the item that was read to them gets added to the appropriate ArrayList. Once done, it prints out all items in the three arrays. It's not working for me, it seems to always go in to the default switch statement, and after I type a number the first time, it reads out 3 more items instead of 1. Yes, it's a mess, I'm looking for some direction but expecting a more learn the basics first kid response.
Here is the code:
class SplitCheck
{
IList byte mylist = new IList
static void Main(string[] args)
{
byte guestlist;
ArrayList Guest1 = new ArrayList();
ArrayList Guest2 = new ArrayList();
ArrayList Guest3 = new ArrayList();
Console.WriteLine("Thank you for dining with us.");
// Create a path to the My Documents folder and the file name
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
Path.DirectorySeparatorChar + "Food.txt";
// Read the contents of the file using ReadAllLines
Console.WriteLine("Please assign each item a guest number");
string[] contents = File.ReadAllLines(filePath);
foreach (string s in contents)
{
Console.WriteLine(s);
guestlist = (byte)Console.Read();
switch (guestlist)
{
case 1 :
//Add to a guest1 array
Guest1.Add(s);
Console.WriteLine("{0} has been added to Guest1", s);
break;
case 2:
//Add to a guest2 array
Guest2.Add(s);
Console.WriteLine("{0} has been added to Guest2", s);
break;
case 3:
//Add to a guest3 array
Guest3.Add(s);
Console.WriteLine("{0} has been added to Guest3", s);
break;
default:
//Add to default array
Console.WriteLine("Default has been used");
break;
}
}
foreach (object o in Guest1)
{
Console.WriteLine("Guest 1 had {0}", o);
}
foreach (object o in Guest2)
{
Console.WriteLine("Guest 2 had {0}", o);
}
foreach (object o in Guest3)
{
Console.WriteLine("Guest 3 had {0}", o);
}
Console.ReadLine();
//Console.WriteLine("Guest 2 had {0}", Guest2());
//Console.WriteLine("Guest 3 had {0}", Guest3());
//Console.ReadLine();
}
}
List<T>instead ofArrayList