4 Replies - 839 Views - Last Post: 11 January 2013 - 01:14 AM Rate Topic: -----

#1 KnowThySelf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-January 13

Why does this not count the letter chosen.

Posted 10 January 2013 - 11:13 PM

So I'm jus tplaying around with arrays trying to get used to them. I decided to create a program that instead of counting the say, a's of text in a rich text box it will count the number of times any letter that appears in your code *of your choosing*.

first of all I'm pretty new to programming just a month or so.
So I figure the problem is in the actual count area, I just don't understand why the code counts every single char in the string instead of counting the char (charLetter1) that i tell it to count in the foreach statement. I tried many different things before I got this far and now I am just stuck on ideas.

 
   public void letterToSearch()
        {
            char charLetter1;
            string strLetter1;
            // incase change array to char array ... { 'a', 'b', 'c', 'd', 'e', 'f' 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
            string[] arrayAlphabet = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
            for (int i = 0; i < 26; ++i)
                if (arrayAlphabet[i] == textboxLetter.Text)
                {
                    strLetter1 = arrayAlphabet[i];
                    charLetter1 = char.Parse(strLetter1);
                   
                    textboxLetterChosen.Text = charLetter1.ToString();
                }
                else { }
        }  //sets the character the user wants to search

        public void countLetters()
        {
            int letterTotal = 0;
            string inputText = textboxLetterCount.Text;
            foreach (char charLetter1 in textboxLetterCount.Text)
            {
                letterTotal++;
            }
            countResultBox.Text = letterTotal.ToString();
        }    //counts and writes the number of letters in that area

        private void btnShowgold_Click(object sender, EventArgs e)
        {
            showPlayersGold();
        }
        private void btnCountLetter_Click(object sender, EventArgs e)
        {
            letterToSearch();
            countLetters();
        }



I attached a picture of the type of results i'm getting.

Is This A Good Question/Topic? 0
  • +

Replies To: Why does this not count the letter chosen.

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5465
  • View blog
  • Posts: 14,418
  • Joined: 18-April 07

Re: Why does this not count the letter chosen.

Posted 10 January 2013 - 11:35 PM

I am not sure why you even have the letterToSearch function. The function does nothing useful. If they enter a value into the textboxletter input box, go right into looping through the characters of the textboxletterCount text and start counting the letters.

No need to find it in some array of letters (as strings), convert it to a char only to convert it BACK to a string and put it in textboxLetterChosen.

char letter = char.Parse(textboxletter.Text);
int letterCount = 0;

for (int i = 0; i < textboxLetterCount.Text.Length; i++) {
    if (letter == textboxLetterCount.Text[i]) {
       letterCount++;
    }
}

return letterCount;



You get the idea from that I am sure. :)
Was This Post Helpful? 2
  • +
  • -

#3 KnowThySelf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-January 13

Re: Why does this not count the letter chosen.

Posted 11 January 2013 - 12:16 AM

I had two methods because I'm just learning about methods too so I thought it'd be cool to make a method that makes sure the char the person inputs is in the array if not it sends an error( i didn't add this yet). And then another

So thank you and I understand your solution its very good. But in the context of what i'm trying to do (just for the hell of it, for now) I guess all i really want to know is WHY does the code give the total amount of letters instead of the letter from the array that the user put in? So that I don't leave here seeing the error in my original ways. is it in the foreach? I swear I thought I had it right

sorry for the incomplete sentence in the first small paragraph there. It should read "and another that counts the total of that chosen char." the array of chars is only important in the event that the user input isn't a letter as defined by the array. Sorry if i'm confusing you.
Was This Post Helpful? 0
  • +
  • -

#4 KnowThySelf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-January 13

Re: Why does this not count the letter chosen.

Posted 11 January 2013 - 12:35 AM

I am not trying to count the number of letters in the textbox I am trying to count the number of letters that the user picks that must be one of the letters in the array. how many a's b's or c's
Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: Why does this not count the letter chosen.

Posted 11 January 2013 - 01:14 AM

you are overwriting your charletter1 defined in the letterToSearch()!!

try that
     public void countLetters()
     {
         int letterTotal = 0;
         string inputText = textboxLetterCount.Text;
         foreach (char blablabla in textboxLetterCount.Text)
         {
             if(blablabla == charletter1)
                 letterTotal++;
         }
         countResultBox.Text = letterTotal.ToString();
     }    //counts and writes the number of letters in that area



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1