Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to create a method to convert the text typed in a box to pig latin when button is pushed. This is the code I have, when I attempt to run it all that appears in output box is
C#
System.Collections.Generic.List`1[System.String]

Where is my error? the method is being called by the btn click:

What I have tried:

Quote:
C#
 private void pigLatin(string sentence) {
            const string vowels = "AEIOUaeio";
            List<string> pigLatin = new List<string>();
            string input = txInput.Text;
            foreach (string word in input.Split(' ')) {
                string firstLetter = word.Substring(0, 1);
                string restOfWord = word.Substring(1, word.Length - 1);
                int currentLetter = vowels.IndexOf(firstLetter);

                if (currentLetter == -1) {
                    pigLatin.Add(restOfWord += firstLetter + "ay");
                }
                else {
                    pigLatin.Add(word + "way");
                }
                txtOutput.Text = " " + pigLatin;
                //return string.Join(" ", pigLatin);
            }
        }

        private void btnConvert_Click(object sender, EventArgs e) {
            string input = (txInput.Text);
            pigLatin(input);
        }
    }
}
Posted
Updated 12-Nov-16 23:23pm
Comments
Philippe Mori 14-Nov-16 15:16pm    
It would not be very hard to add an example of expected conversion and to tell on which line the error occurs.

pigLatin is a List you can't just call ToString() on it, try the following instead:
C#
...
txtOutput.Text = string.Join(", ", pigLatin.ToArray()); // will make a comma list
                     // or  "\r\n" -> for line separated 
...
 
Share this answer
 
Is the output or an error message ?
To see what your code is doing, use the debugger.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900