Home > OS >  Trying to sort a List with dynamic List Size, having Unhandled Exceptions
Trying to sort a List with dynamic List Size, having Unhandled Exceptions

Time:05-29

I'm trying to creat an algorithm who receive a list of user-defined size (dynamic list) but i keep having unhandled exceptions when trying to implement the algorithm.

The cleanest i found was this

using System.Linq;
using System.Collections.Generic;


static void Sorter()
{
    Console.WriteLine("Write the number of words the dictionary will have");
   int Size = Convert.ToInt32(Console.ReadLine());
   int ListEnd = Size;
    List<string?> wordList = new() {};
   while (Size>0)
    {
       Console.WriteLine("Write a term A");
      string? item1 = "" ?? "1";
       item1 = Console.ReadLine();
       wordList.Add(item1);
       Size--;
    }
    Console.WriteLine($"{wordList.Count()} terms");
    Console.WriteLine(string.Join(" ", wordList));
    string? last = wordList[ListEnd];
    wordList.Sort();

    Console.WriteLine(string.Join(" ", wordList));
}

I want the program to receive all the unsorted names written by the user and then reposition then in the List in a alphabetically sorted way.

CodePudding user response:

First of all, if you want to declare an empty list, you can do it with:

List<string?> wordList = new List<string?>();

Then, A namespace cannot directly contain members such as fields or methods. Thats why you need to wrap the function by a class:

public class AnyClassName
{
    static void Sorter()
    {
        Console.WriteLine("Write the number of words the dictionary will have");
       int Size = Convert.ToInt32(Console.ReadLine());
       int ListEnd = Size - 1;
        List<string?> wordList = new List<string?>();
       while (Size>0)
        {
           Console.WriteLine("Write a term A");
          string? item1 = "" ?? "1";
           item1 = Console.ReadLine();
           wordList.Add(item1);
           Size--;
        }
        Console.WriteLine($"{wordList.Count()} terms");
        Console.WriteLine(string.Join(" ", wordList));
        string? last = wordList[ListEnd];
        wordList.Sort();
    
        Console.WriteLine(string.Join(" ", wordList));
    }
}

Then, you need to call the function in main function:

using System;
using System.Linq;
using System.Collections.Generic;
public class AnyClassName
{
    static void Sorter()
    {
        Console.WriteLine("Write the number of words the dictionary will have");
       int Size = Convert.ToInt32(Console.ReadLine());
       int ListEnd = Size - 1;
        List<string?> wordList = new List<string?>();
       while (Size>0)
        {
           Console.WriteLine("Write a term A");
          string? item1 = "" ?? "1";
           item1 = Console.ReadLine();
           wordList.Add(item1);
           Size--;
        }
        Console.WriteLine($"{wordList.Count()} terms");
        Console.WriteLine(string.Join(" ", wordList));
        string? last = wordList[ListEnd];
        wordList.Sort();
    
        Console.WriteLine(string.Join(" ", wordList));
    }
    public static void Main(string[] args)
    {
       Sorter();
    }
}

CodePudding user response:

A couple pointers, as pointed out in the comments of your original post (pun not intended):

  • Try to include any errors when stating that you are seeing errors.
  • Try to specify what you are actually looking for. Are you looking for a cleaner implementation? Are you looking for a fix to an error (if so, refer to the previous pointer)?

As for the direct answer to your question, I am assuming that you are looking for a cleaner implementation that doesn't result in errors during compilation. Here we go:

using static System.Int32;

List<string> ReadStringsFromConsole()
{
    int          numWords = 0;
    string       word     = string.Empty;
    List<string> words    = new();

    // Keep on reading until we get a valid number
    while (numWords < 1)
    {
        Console.Clear();
        Console.Write("How many words would you like to enter? ");
        bool unused = TryParse(Console.ReadLine(), out numWords); // TryParse to make sure we don't get an exception
    }

    // Keep on reading until we get a valid word, at which point it is added to the list
    // numWords is only decremented if we were able to successfully add the word to the list
    do
    {
        Console.Clear();
        Console.Write("Enter a word: ");
        word = Console.ReadLine() ?? string.Empty;

        if (word == string.Empty) continue;
        words.Add(word);
        numWords--;
    } while (numWords > 0 && word != string.Empty);

    words.Sort();
    return words;
}

List<string> words = ReadStringsFromConsole();

Console.WriteLine(string.Join(" ", words));
Console.ReadKey();
  • Related