Guest_imported
New member
- Jan 1, 1970
- 0
Hi.Here is my question:
This program takes one command-line argument that specifies a person's name.It than searches through a two-dimensional array of strings for that name.If it finds a match,it displays that person's telephone number.This program is taken from a book.
public static int Main(string[] args)
{
string[,] numbers=
{
{"Tom","111"},
{"Mary","222"},
{"John","333"},
{"",""}
};
int i;
if(args.Length !=1)
{
Console.WriteLine("Not correct"
return 1;
}
else
{
for(i=0;numbers[i,0]!="";i++)
{
if(numbers[i,0]==args[0])
{
Console.WriteLine(numbers[i,1]);
break;
}
}
}
return 0;
}
If I write on a command-line as an argument one of the names that are also present in a string array or if I don't pass any arguments at all,than the program works fine.
But if I pass as an argument anything else,lets say 'Will' than I get 'INDEX-OUT-OF-RANGE-EXCEPTION'.Why is that?The loop should continue only untill numbers[i,0]!="" and than it should STOP.
Thank you for all your help!
This program takes one command-line argument that specifies a person's name.It than searches through a two-dimensional array of strings for that name.If it finds a match,it displays that person's telephone number.This program is taken from a book.
public static int Main(string[] args)
{
string[,] numbers=
{
{"Tom","111"},
{"Mary","222"},
{"John","333"},
{"",""}
};
int i;
if(args.Length !=1)
{
Console.WriteLine("Not correct"
return 1;
}
else
{
for(i=0;numbers[i,0]!="";i++)
{
if(numbers[i,0]==args[0])
{
Console.WriteLine(numbers[i,1]);
break;
}
}
}
return 0;
}
If I write on a command-line as an argument one of the names that are also present in a string array or if I don't pass any arguments at all,than the program works fine.
But if I pass as an argument anything else,lets say 'Will' than I get 'INDEX-OUT-OF-RANGE-EXCEPTION'.Why is that?The loop should continue only untill numbers[i,0]!="" and than it should STOP.
Thank you for all your help!