0

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();
    }
}
3
  • 2
    If you are using .Net 2.0 or higher then use List<T> instead of ArrayList Commented Dec 9, 2014 at 21:55
  • 1
    Why are you reading in the input as a byte instead of a string? Commented Dec 9, 2014 at 21:59
  • What is your question? I see 1 compile error and one logic error. Commented Dec 9, 2014 at 21:59

4 Answers 4

1

guestlist should not be of type byte, you want it of type char. Try that, and if you are still stuck read on:

Hint #2: Enter a 1, then put a breakpoint on the value of guestList. What is the value? It's 49. What is significant about that number? Hint: Think ASCII.

So try this instead:

char 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.MyDocum
                            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 = (char)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;
Sign up to request clarification or add additional context in comments.

Comments

0

Your issue is here

guestlist = (byte)Console.Read();

change it to

guestlist = Convert.ToInt32(Console.ReadLine());

You are casting the console read data as a byte, updated based on feedback.

2 Comments

That has the exact same problem. As written, the value of guestList will be 49 when the user types '1'
@MobyDisk missed that part thank you, answer updated.
0

The value being read in is the ASCII representation of the character, not the actual character. In addition, the Console.Read method will also read in the newline when the person presses the ENTER key, which will cause the loop to go three times (the character, then the carriage return and newline).

I suggest using int.TryParse to check if the input is valid, to convert the input to an int type, and finally, using Console.ReadLine to eliminate the newlines when the person presses ENTER.

        int val;
        var success = int.TryParse(Console.ReadLine(), out val);

        if (success)
        {
            switch (val)
            {
                case 1:
                    Console.WriteLine("has been added to Guest1");
                    break;
                case 2:
                    Console.WriteLine("has been added to Guest2");
                    break;
                case 3:
                    Console.WriteLine("has been added to Guest3");
                    break;
                default:
                    Console.WriteLine("Default has been used");
                    break;
            }
        }
        else
        {
            Console.WriteLine("Invalid value");
        }

Comments

0

You could instead directly inspect the result of Console.ReadKey instead of interpreting the integer value from Console.Read(), and simply update a dictionary without needing the switch statement at all:

var guestCounts = new Dictionary<int, List<string>>
{
    { 1, new List<string>() },
    { 2, new List<string>() },
    { 3, new List<string>() }
}

// Collect inputs
foreach (string food in File.ReadAllLines(filePath))
{
    int guest;
    if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out guest) &&
        guestCounts.ContainsKey(guest))
    {
        guestCounts[guest].Add(food);
        Console.WriteLine("{0} has been added to Guest{1}", food, guest);
    }
    else
    {
        Console.WriteLine("Default has been used.");
    }
}

// Output results
foreach (int guest in guestCounts.Keys)
{
    foreach (string food in guestCounts[guest])
    {
        Console.WriteLine("Guest {0} had {1}", guest, food);
    }
}

3 Comments

I don't think that compiles. Console.Read() returns an int, and int.TryParse takes a string.
Could you change it to ReadLine()?
@MobyDisk: I updated my answer to another alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.