Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop a Console.ReadLine?

Tags:

c#

I cannot figure out how to read user-input in a loop (with Console.ReadLine). I'm trying to create a note that lets me store what ever the user inputs, and exits if he types exit.

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            Note myNote = new Note();
            Note otherNote = new Note();
            myNote.addText("Hi there");
            Console.WriteLine(myNote.display());
            otherNote.addText(Console.ReadLine());
            Console.WriteLine(otherNote.display());
            if (otherNote = "exit")
            {

            }

        }
    }


}


    class Note
{
    private string text = "";
    private DateTime timeStamp = DateTime.Now;
    private DateTime modifiedStamp = DateTime.Now;
    int maxLength = 10;


    public void addText(string sometext)
    {
        if (text.Length + sometext.Length < maxLength)
        {
            text += sometext;
            modifiedStamp = DateTime.Now;
        }

    }

    public string display()
    {
        return "Created: " + timeStamp.ToString() + "\n" +
            "Modified: " + modifiedStamp.ToString() + "\n" +
            "Content: " + text;
    }
}
like image 939
ed M Avatar asked Mar 01 '26 02:03

ed M


2 Answers

You need List of Notes in order to add as many notes as you want. Additionally, you need to first save ReadLine input check if the user really asked to exit otherwise keep adding notes.

var myNotes = new List<Note>();
var firstNote = new Note();
firstNote.addText("Hi there");

Note note;
while (true)
{
    var input = Console.ReadLine();
    if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
    {
        break;
    }
    note = new Note();
    note.addText(input);
    myNotes.Add(note);
}
like image 188
Orel Eraki Avatar answered Mar 03 '26 16:03

Orel Eraki


The general format is to use something like this (a while loop with a break condition):

// put code above while loop that only needs to be executed once
while (true) {        
    // get the user input for every iteration, allowing to exit at will
    String line = Console.ReadLine();
    if (line.Equals("exit")) {
        // exit the method.
        return; // use "break" if you just want to exit the loop
    }
    // this is what will happen in the loop body since we didn't exit
    // put whatever note stuff you want to execute again and again in here
}

You'll want to edit what goes into the body of this loop depending on what exactly you want done with your note instances. But generally, you repeatedly prompt a user for input until some condition is met and then you break out of the loop. You may decided that condition (e.g. "enter 10 notes"; "type exit"; etc.)

like image 29
Ian R. O'Brien Avatar answered Mar 03 '26 14:03

Ian R. O'Brien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!