skip to Main Content

I am working on a school project and the task was to create an application, which one of the features is to save multi-lined text (user input) into a string. I know some parts of the code might be outdated, but our school compiler only supports .NET version 4 (edit: I compile in Visual Studio 2022, school compiler is only the place where we showcase the final work, that is why the version is a limit for finished work). I am still learning and I am sure there are other things that could be improved, but let’s look at the main problem.

When the user types standard Czech text (for example "včera", which means yesterday), the text saved and later returned shows "vera". If I type only these special characters, for example "ůčšěž", which doesn’t mean anything, the string content is left empty.
When I create a string using different method, for example basic Console.WriteLine(), the special characters are saved just okay.
The method I am using is supposed to take user input and supply that multi-lined text together with DateTime to an object Record inside a doubly linked list. Below, I will show the relevant code because the mistake could be created on the way.

public static void CreateNewRecord()
{
    Console.Write("Date: ");
    if (DateTime.TryParse(Console.ReadLine(), out DateTime parsedDate))
    {
        Console.WriteLine("nText: ");

        StringBuilder sb = new StringBuilder();

        while (true)
        {
            string textLine = Console.ReadLine();
            if (textLine.ToLower() == "save")
                break;
            sb.AppendLine(textLine);
        }
        string recordText = sb.ToString();
        Program.records.AddLast(new Record(parsedDate, recordText));
    }
    else
    {
        Console.WriteLine("Wrong date format.");
    }
}

This creates the Record and and using the overriden ToString(), I am trying to read it:

public override string ToString()
{
    return string.Format("Date: {0}nn{1}", Date, Text);
}

However, when trying to read the content using Console.WriteLine(records.Last());, only english-lettered text is returned.
If anyone has idea what is wrong, your answer will be welcomed.

I tried to add UTF-8 encoding to the top of my program, which didn’t help. I tried to create a very simple string builder that appends parts of text (not whole lines), and it returned even with the special characters.

I expect a multi-lined text to be returned with all the symbols the user saves to it.

3

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone for your time. I tried to run the code on different computer and the error does not show up there. Special characters are saved. It looks like I have my system console corrupted. I do not exactly know where the error is, but it is not error in the code, therefore not suitable for this forum. Thank again, everyone.


  2. To help you handle special characters properly:

    1. Set Console Encoding: Before reading user input and writing to the console, you should set the console’s encoding to UTF-8 to handle special characters correctly. You can do this at the beginning of your program:
    Console.OutputEncoding = Encoding.UTF8;
    Console.InputEncoding = Encoding.UTF8;
    
    1. Update Your CreateNewRecord Method: Ensure that you read user input and store it correctly with UTF-8 encoding. You can modify your CreateNewRecord method like this:
       public static void CreateNewRecord()
       {
           Console.Write("Date: ");
           if (DateTime.TryParse(Console.ReadLine(), out DateTime parsedDate))
           {
               Console.WriteLine("nText: ");
    
               StringBuilder sb = new StringBuilder();
    
               while (true)
               {
                   string textLine = Console.ReadLine();
                   if (textLine.ToLower() == "save")
                       break;
                   sb.AppendLine(textLine);
               }
               string recordText = sb.ToString();
               Program.records.AddLast(new Record(parsedDate, recordText));
           }
           else
           {
               Console.WriteLine("Wrong date format.");
           }
       }
    
    1. Ensure Your Record Class Handles Special Characters: Make sure that your Record class properly handles special characters when storing and retrieving text. If your Record class uses a string to store the text, it should work correctly with UTF-8 encoded strings by default. With these changes, your program should handle multi-lined text with special characters correctly. Ensure that the console encoding is set to UTF-8, and special characters should be preserved when saving and displaying the text.
    Login or Signup to reply.
  3. The issue you’re facing might be related to the encoding used when reading and writing the text. To ensure proper handling of special characters, you can try using the Console.OutputEncoding property to set the encoding to UTF-8 before writing the text. Here’s an example of how you can modify your code:

    public static void CreateNewRecord()
    {
        Console.Write("Date: ");
        if (DateTime.TryParse(Console.ReadLine(), out DateTime parsedDate))
        {
            Console.WriteLine("nText: ");
    
            StringBuilder sb = new StringBuilder();
    
            while (true)
            {
                string textLine = Console.ReadLine();
                if (textLine.ToLower() == "save")
                    break;
                sb.AppendLine(textLine);
            }
            string recordText = sb.ToString();
    
            // Set the console output encoding to UTF-8
            Console.OutputEncoding = Encoding.UTF8;
    
            Program.records.AddLast(new Record(parsedDate, recordText));
        }
        else
        {
            Console.WriteLine("Wrong date format.");
        }
    }
    

    By setting the Console.OutputEncoding to Encoding.UTF8, you ensure that the console can handle and display the special characters properly.
    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search