0

I need to appned .txt to user input,like course and put in to the file,looks like i managed to do it ,but in second part need arraylist and insert into it subjects and then right to the text file and ouput/display. For some reason the 'exit' is diplayed too and no subjects copied into arraylist.What did i missed?

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

namespace UserInput
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter new couse name  - type exit and press enter to finish editing:");
            string newContent = Console.ReadLine();
            while (newContent != "exit")
            {
                File.AppendAllText("Info.txt", newContent + Environment.NewLine);
                newContent = Console.ReadLine();
                Console.WriteLine("Please enter new course subject  - type exit and press enter to finish editing:");
                string newContent1 = Console.ReadLine();

                ArrayList list = new ArrayList();
                list.Add(newContent1);
                File.AppendAllText("Info.txt", newContent + Environment.NewLine);
                newContent = Console.ReadLine();
            }

            string[] linesIn = System.IO.File.ReadAllLines(@"Info.txt");

            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of Info.txt = ");
            foreach (string line in linesIn)
            {
                // Use a tab to indent each line of the file.
                Console.WriteLine("\t" + line);
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();





        }
    }
}
3
Contributors
4
Replies
30
Views
2 Years
Discussion Span
Last Post by ddanbe
0

The Arraylist is empty, because you start with a new one each time you get input from the user. Declare it just before the start of the while loop.

0

If i place array list infront of the while,then it does not output content of array list , only firt part entered. And strange why does it count exit as input?

1

No where in your code do you try to read the arraylist. Also you've told the user to enter exit and the first ReadLine() following is being written to the file.

1

It was easier to let the code speak than to explain what needed to be done to correct the code in your while loop:

namespace UserInput
{
    class Program
    {
        static void Main(string[] args)
        {
            const string stop = "exit";
            //used List instead of ArrayList
            List<string> CourseList = new List<string>();

            ShowPromt();
            string newContent = Console.ReadLine();
            while (newContent != stop) //revised while loop
            {
                CourseList.Add(newContent);
                File.AppendAllText("Info.txt", newContent + Environment.NewLine);               
                ShowPromt();
                newContent = Console.ReadLine();
            }

            string[] linesIn = System.IO.File.ReadAllLines(@"Info.txt");

            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of Info.txt = ");
            foreach (string line in linesIn)
            {
                // Use a tab to indent each line of the file.
                Console.WriteLine("\t" + line);
            }
            System.Console.WriteLine("Contents of the course list = ");
            foreach (string course in CourseList)
            {
                // Use a tab to indent each item of the list.
                Console.WriteLine("\t - {0}" , course);
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }

        static void ShowPromt()
        {
            Console.WriteLine("Please enter new course name  - type exit and press enter to finish editing:");
        }
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.