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();





        }
    }
}

Recommended Answers

All 4 Replies

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.

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?

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.

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:");
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.