Ettur
Ettur

Reputation: 789

No errors in Visual Studio, Code Executes but something not right C# (updated! problem in String.Insert)

I'm writing a program for schoolwork, that's supposed to generate a 10 000 "movie" list. A single "movie" consist of a string in a form "moviename year director". I say "movie" because movie name and director are supposed to be randomly generated with letters from a-z.

I wrote the following logic to generate one such "movie". Movie name and director are random letter combination in length between 4-10 charachters. Code gives no errors in visual studio, executes, but shows blank. If I wrote correctly, then this code should generate one such string and print it, yet console is blank.

Do while loop is there to check if, however unlikely, there is a double item in the List (this is for when I do the 10 000 version).

In short, I dont understand what am I doing wrong?

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

namespace Experiment
{
  class Program
  {
    static void Main(string[] args)
    {
        Movies();
        Console.ReadKey();
    }

    public static void Movies()
    {
        List<string> movieList = new List<string>();
        bool check = false;

        do
        {
            string movie = "";

            for (int i = 0; i < GetNum(); i++)
            {
                movie.Insert(0, Convert.ToString(GetLetter()));
            }

            movie.Insert(0, " ");
            movie.Insert(0, Convert.ToString(GetYear()));
            movie.Insert(0, " ");

            for (int i = 0; i < GetNum(); i++)
            {
                movie.Insert(0, Convert.ToString(GetLetter()));
            }

            if (movieList.Contains(movie))
            {
                check = false;
            }
            else
            {
                movieList.Add(movie);
                check = true;
            }

        } while (check == false);

        Console.WriteLine(movieList[0]);


    }


    public static Random _random = new Random();

    public static char GetLetter()
    {
        int num = _random.Next(0, 26);
        char let = (char)('a' + num);
        return let;
    }

    public static int GetNum()
    {
        int num = _random.Next(4, 11);
        return num;
    }

    public static int GetYear()
    {
        int num = _random.Next(1920, 2020);
        return num;
    }

   }
 }

Upvotes: 0

Views: 76

Answers (3)

S.N
S.N

Reputation: 5140

Insert() method is used to return a new string from the specified string at a specified index position. In your case, you are not capturing the updated string.

The best approach to solve this is through using StringBuilder object. Please note that StringBuilder object is much efficient rather than playing with immutable string.

Upvotes: 0

jason.kaisersmith
jason.kaisersmith

Reputation: 9610

The problem is that you are using movie.Insert incorrectly.

If you read the documentation for String.Insert it says

https://learn.microsoft.com/en-us/dotnet/api/system.string.insert?view=netframework-4.8

Returns a new string in which a specified string is inserted at a specified index position in this instance

public string Insert (int startIndex, string value);

So it returns a new String, it does not amend the existing one. So you would need to do.

movie = movie.Insert(0, Convert.ToString(GetYear()));

However, I must also say that using String.Insert in this way is not the best approach. You should instead look at using the StringBuilder class. It is very efficient when you want to amend strings (which are immutable objects).

You might want to read parts of this to help you understand. If you scroll down then it also suggests StringBuilder.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/

Upvotes: 3

Wim
Wim

Reputation: 12082

Strings are immutable, so calling the Insert() method on the movie string doesn't do anything to the current movie variable. Instead it returns the new string.

You are however better off changing the movie type from string to StringBuilder, which is a dynamically allocated buffer of characters, so your example becomes:

using System;
using System.Text;
using System.Collections.Generic;

namespace sotest
{
    class Program
    {
        static void Main(string[] args)
        {
            Movies();
            Console.ReadKey();
        }

        public static void Movies()
        {
            List<string> movieList = new List<string>();
            bool check = false;

            do
            {
                StringBuilder movie = new StringBuilder();

                for (int i = 0; i < GetNum(); i++)
                {
                    movie.Insert(0, Convert.ToString(GetLetter()));
                }

                movie.Insert(0, " ");
                movie.Insert(0, Convert.ToString(GetYear()));
                movie.Insert(0, " ");

                for (int i = 0; i < GetNum(); i++)
                {
                    movie.Insert(0, Convert.ToString(GetLetter()));
                }

                if (movieList.Contains(movie.ToString()))
                {
                    check = false;
                }
                else
                {
                    movieList.Add(movie.ToString());
                    check = true;
                }

            } while (check == false);

            Console.WriteLine(movieList[0]);


        }


        public static Random _random = new Random();

        public static char GetLetter()
        {
            int num = _random.Next(0, 26);
            char let = (char)('a' + num);
            return let;
        }

        public static int GetNum()
        {
            int num = _random.Next(4, 11);
            return num;
        }

        public static int GetYear()
        {
            int num = _random.Next(1920, 2020);
            return num;
        }

    }
}

Upvotes: 3

Related Questions