Jagged array-Read values from keyboard

Posted by Sina under C# on 7/21/2011 | Points: 10 | Views : 6829 | Status : [Member] | Replies : 5
Hi i want a help infact clarification of a doubt. I want to know how to read values into a jagged array through keyboard just like we enter values in arrays. Everywher its mentioning only initialization ,i.e feeding the values through code itself. I want to get values from user. How would the coding be? Please help me.
class Program
{
static void Main(string[] args)
{
string[][] stringarr1 = new string[15][];
for (int i=0;i<stringarr1.Length;i++)
{
stringarr1[i]=Console.ReadLine();
}
for(int j=0;j<stringarr1.Length;j++)
{
Console.WriteLine(stringarr1[j]);
}
}
}

This is the code i wrote to read and display strings. But it shows error in the bold statement.

Hoping to receive immediate reply.
Thank you!!!

@Sina


Responses

Posted by: SheoNarayan on: 7/21/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Its little different, read this http://www.dotnetfunda.com/codes/code1343-create-jagged-array-.aspx.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sina on: 7/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi thanks for the reply but here too we are initializing the array. I want to read values from keyboard using Console.ReadLine() method.

@Sina

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jpchoudhari on: 7/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Hope this helps you

Console.Write("Enter no of rows : ");
int iNoOfRows = Convert.ToInt32(Console.ReadLine());
string[][] stringarr1 = new string[iNoOfRows][];
Console.WriteLine("\n");
for (int i = 0; i < stringarr1.Length; i++)
{
Console.Write("No of columns for row " + (i + 1) + " : ");
stringarr1[i] = new string[Convert.ToInt32(Console.ReadLine())];
}
Console.WriteLine("\n");
for (int i = 0; i < stringarr1.Length; i++)
{
for (int j = 0; j < stringarr1[i].Length; j++)
{
Console.Write("Enter value for Row " + (i + 1) + ",Column " + (j + 1) + " : ");
stringarr1[i][j] = Console.ReadLine();
}
Console.WriteLine();
}

Console.WriteLine("Reading values..\n");

for (int i = 0; i < stringarr1.Length; i++)
{
for (int j = 0; j < stringarr1[i].Length; j++)
{
Console.Write(stringarr1[i][j].ToString() + "\t\t");

}
Console.WriteLine();
}

Console.WriteLine("\n\nPress any key to continue........");
Console.ReadLine();

Kind Regards,
Jay

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sina on: 7/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi i am getting the same error that i got for my code..Please find the screenshot attached for your reference
 Download source file

@Sina

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jpchoudhari on: 7/21/2011 [Member] Starter | Points: 25

Up
0
Down
yeas actually this is a problem of code editor of this site.

you have to add index positions properly.

find the attachment.
 Download source file

Kind Regards,
Jay

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response