-1

How can I replace more than one occurrence of an input letter using indexOf? I can currently find all index occurrences if they end in -1. I'm also getting an unhanded exception when converting to char.

static Random rnd = new Random();
    static void Main(string[] args)
    {
        string wrong = "";

        ArrayList list = new ArrayList();
        list.Add("Dune");
        list.Add("The Lord of the Rings");
        list.Add("The Iliad");
        list.Add("Hamlet");

        int r = rnd.Next(list.Count);
        Console.WriteLine((string)list[r]);
        string s = (string)list[r];

        string patten = "[a-zA-Z0-9]";
        Regex rgx = new Regex(patten);

        string sDash = rgx.Replace(s, "-");

        do
        {
            string a = " ";

            Console.Write("Guess a letter: ");
            a = Console.ReadLine();

            while (a.Length != 1)
            {
                Console.WriteLine("Please enter only one letter:");
                a = Console.ReadLine();
            }

            char myChar = a[0];
            int input = s.IndexOf(a);
            while (input != -1)
            {
                Console.WriteLine(input);
                input = s.IndexOf(myChar, input + 1);
            }

            if (input != -1)
            {
                StringBuilder builder = new StringBuilder(sDash);
                builder[input] = myChar;

                sDash = builder.ToString();
                Console.WriteLine(sDash + String.Compare(sDash, s, true));
            }
            else {
                StringBuilder builder = new StringBuilder(wrong);
                builder.Append(a);
                string wrongAnswers = builder.ToString();
                Console.WriteLine("Wrong, try again. \n {0}", wrongAnswers);
            }
        } while (String.Compare(sDash, s) == -1);

    }
4444
  • 3,541
  • 10
  • 32
  • 43
user2390516
  • 17
  • 1
  • 1
  • 6
  • 6
    Don't use `ArrayList` anymore but a `List`. – Tim Schmelter Jun 05 '14 at 14:54
  • What is your exact problem? Where does this exception occur and what is the exact exception? – Sayse Jun 05 '14 at 15:01
  • @user2390516: it won't solve your issue (that's why it's a comment) but it'll help to write more robust code. http://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp – Tim Schmelter Jun 05 '14 at 15:02
  • Try Convert.ToChar(a); – Bit Jun 05 '14 at 15:04
  • Why don't you use Console.ReadKey() http://msdn.microsoft.com/en-us/library/471w8d85%28v=vs.110%29.aspx instead of Console.ReadLine() this will automatically give you one caracter and the character can be easely obtained with Console.ReadKey().KeyChar : http://msdn.microsoft.com/en-us/library/system.consolekeyinfo.keychar%28v=vs.110%29.aspx – Hybris95 Jun 05 '14 at 15:04

1 Answers1

0

This is how I would do :

static Random rnd = new Random();
static void Main(string[] args)
{
    string wrong = "";

    List<String> list = new List<String>();
    list.Add("Dune");
    list.Add("The Lord of the Rings");
    list.Add("The Iliad");
    list.Add("Hamlet");

    int r = rnd.Next(list.Count);
    Console.WriteLine((string)list[r]);
    string s = (string)list[r];

    string patten = "[a-zA-Z0-9]";
    Regex rgx = new Regex(patten);

    string sDash = rgx.Replace(s, "-");
    StringBuilder wrongAnswers = new StringBuilder();
    do
    {
        string a = " ";

        Console.Write("Guess a letter: ");
        a = Console.ReadLine();

        while (a.Length != 1)
        {
            Console.WriteLine("Please enter only one letter:");
            a = Console.ReadLine();
        }

        char myChar = a.ToLower()[0];
        int input = s.ToLower().IndexOf(myChar);
        bool foundSomething = false;
        StringBuilder builder = new StringBuilder(sDash);
        while(input != -1)
        {
            foundSomething = true;
            builder[input] = myChar;
            try
            {
                input = s.ToLower().IndexOf(myChar, input + 1);
            }
            catch (ArgumentOutOfRangeException)
            {
                input = -1;
            }
        }
        sDash = builder.ToString();

        if (foundSomething)
        {
            Console.WriteLine(sDash + " " + sDash.ToLower().Equals(s.ToLower()));
        }
        else
        {
            wrongAnswers.Append(myChar);
            Console.WriteLine("Wrong, try again. \n {0}", wrongAnswers);
        }
    } while (!sDash.ToLower().Equals(s.ToLower()));

}

By the way, there are many optimization that can be done here, but the principle is here..

Hybris95
  • 2,286
  • 2
  • 16
  • 33
  • Thank you so very much. you fixed allot of my issues and added a case insensitive search which was next on the list. Thank you! I'm most certainly sure of the optimization opportunitys in my code. I'm just using a hangman app to teach myself C#. thank you so much for your help – user2390516 Jun 05 '14 at 16:10