How to resolve a string formatting error?

Asked

Viewed 2,249 times

0

I was programming normally in VS in "Console" mode when I came across this message:

"A first chance Exception of type 'System.Formatexception' occurred in mscorlib.dll An unhandled Exception of type 'System.Formatexception' occurred in mscorlib.dll Additional information: Index (based on zero) must be greater than or equal to zero and smaller than the argument list size. The program '[1124] Consoleapplication4.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0)"

What it’s all about and how to fix it?

Code:

string A1;
string A2;
string A3;
string A4;
string A5;
String A6;
String A7;
String A8;


A1 = Console.ReadLine();
A2 = Console.ReadLine();
A3 = Console.ReadLine();
A4 = Console.ReadLine();
A5 = Console.ReadLine();
A6 = Console.ReadLine();
A7 = Console.ReadLine();
A8 = Console.ReadLine();

Console.Write("Segunda: {0}, {1}, {2}, {3}", A1, A2, A3, A4);
Console.ReadLine();
Console.Write("Terça: {4}, {7}, {5}, {1}, {6}", A5, A8, A6, A2, A7); 

Console.ReadKey();
  • When debugging, post the code from where the exception was triggered, so we can point out the problem. By error msg, the problem is in one of the parameters you passed to some function, or a negative value, or a value greater than the allowed.

  • Passes more information, Like the debug shown above, Or the snippet of your code that generates this Exception, Only the error message is very vague.

2 answers

6


The problem is that you put the argument that does not exist. These numbers inside the keys are the number in the order that comes arguments after the formatting text. The first will always be 0 and go, 1, 2, and so on as many as you have, so the last will be the number of arguments passed minus 1. If you passed 4, they will go from 0 to 3. You cannot use the numbers you used. The error occurred because it did not find an argument number 4. A string formatting is having problems in this context, so it generates an exception at runtime.

But we can do it in a simpler way and avoid this error. We can modernize this code by greatly simplifying it. You do not need to declare the variable before assigning a value to it, you do not need to say its type, and you do not need to pass arguments after the string formatting.

Use var to make inference, using static to import static class, and interpolation of string to use arguments within the string. It gets better:

using static System.Console;

public class Program {
    public static void Main() {
        var A1 = ReadLine();
        var A2 = ReadLine();
        var A3 = ReadLine();
        var A4 = ReadLine();
        var A5 = ReadLine();
        var A6 = ReadLine();
        var A7 = ReadLine();
        var A8 = ReadLine();
        Write($"Segunda: {A1}, {A2}, {A3}, {A4}");
        ReadLine();
        Write($"Terça: {A5}, {A8}, {A6}, {A2}, {A7}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

4

Your mistake is on this line:

Console.Write("Terça: {4}, {7}, {5}, {1}, {6}", A5, A8, A6, A2, A7);

The indexes in this Console.Write should start at 0 and go up to at most the number of parameters minus 1. To correct, just change the indexes of the writing parameters, thus:

Console.Write("Terça: {0}, {1}, {2}, {3}, {4}", A5, A8, A6, A2, A7);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.