2

I'm trying to complete a practice assignment that was due a few weeks ago(not graded), and I cannot for the life of me figure it out what is wrong. I need the output to be " 1 The 2 quick 3 brown 4 fox 5 jumps 6 over 7 the 8 lazy 9 dog" I have the split string so I can get all the words down, just cannot get the numbers before it. so far I have this

int ctr = 0;
            string sentance = "The quick brown fox jumped over the lazy dog. ";
            string[] split = sentance.Split(new char[] { ' ', '.' });
            foreach (string s in split)
            {
                if (s.Trim() != "")
                    Console.WriteLine("{0} {2}", ctr++, s);
            }
            Console.ReadLine();

All I keep getting is an unhandled exception, and I cannot find out why.

4
  • The output is supposed to be a new line starting with each number Commented Jan 24, 2019 at 4:52
  • do print the entire details (System.FormatException) in your question. unhandled just mentioned there is an exception Commented Jan 24, 2019 at 5:04
  • side note. there is also a function string.IsNullOrWhiteSpace Commented Jan 24, 2019 at 5:17
  • Where you printing output it should be {1} not {2}. Or you can use string interpolation format like Console.WriteLine($"{ctr++} {s}"). Note this feature is available from C# 6. Commented Jan 24, 2019 at 6:03

4 Answers 4

3

In the line

Console.WriteLine("{0} {2}", ctr++, s);

{0} refers to the zeroth element in the list that follows; that would be ctr++. {1}, if you used it, would refer to the next one, s. {2}, which you did use, would refer to the next one, if there was a next one, but there isn't; and that's your problem.

There is no #2 variable (counting 0, 1, 2) to fill in for {2}.

Try

Console.WriteLine("{0} {1}", ctr++, s);

and you should see {1} picking up s.

Your error message is probably something like "Index (zero based) must be greater than or equal to zero and less than the size of the argument list"

This means your index 2 in {2} is the problem. It meets the first part ("greater than or equal to zero") but fails the second part ("less than the size of the argument list") because your argument list is only 2 long (ctr++ and s), but 2 (your index in {2}) is not less than 2 (the size of the argument list).

You also have an off-by-one problem with the value of ctr, but this should be enough to put you back on track.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I can't believe it's that simple, working on it for 4 hours I guess I over looked it! I'll fix it soon!
Glad to help. If you found it helpful, please accept it as an answer.
0

I appended each element of the string array to a string 'result' and then printed the whole thing post the for loop.

I have tried to do the concatenation in a simple way, but you can use some other functions to make this prettier, and not so manual anyway hope that helps.

        string sentence = "The quick brown fox jumped over the lazy dog. ";
        string[] split = sentence.Split(new char[] { ' ', '.' });
        string result = "";
        int i = 1;
        foreach (string s in split)
        {
            if (s.Trim() != ""){
                result += (i + " " + s + " ");
                i++;
            }
        }
        Console.WriteLine(result);

Comments

0

Your code seems fine. You simply need to use "{0} {1}" instead of "{0} {2}" since you need the indexes 0 and 1. Another interesting option is using the Linq library. To make sure your output goes "1... 2... 3..." and not "0.... 1... 2..." you need to make sure you use ++ctr not ctr++. This makes sure that the value is returned after the increment instead of before.

Using System.Linq library you can simply use the following:

string sentance = "The quick brown fox jumped over the lazy dog. ";
string[] split = sentance.Split(new char[] { ' ', '.' });
string result = string.Join(" ", from w in split 
                                    where w.Trim() != ""
                                    select ++ctr + " " + w);

2 Comments

Thank you! I can't believe it's that simple, working on it for 4 hours I guess I over looked it! I'll fix it soon!
do have a look at LINQ. it makes things really simple.
0

Your code should look like below:

int ctr = 0;
string sentance = "The quick brown fox jumped over the lazy dog. ";
string[] split = sentance.Split(new char[] { ' ', '.' });

foreach (string s in split)
{
    if (!string.IsNullOrWhiteSpace(s))
        Console.WriteLine("{0} {1}", ctr++, s);
}

Console.ReadLine();

More on string.IsNullOrWhiteSpace here.

Also, String.Split comes with overloads. One of them is RemoveEmptyEntries. If you need to remove empty entries and avoid check for Trim() etc, then can use it. More on it here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.