I have this array: public string[,] computer = new string[10, 13];
and i want to put values in the array like this:
computer[1, 1] = Console.ReadLine();
and it doesnt work for me! please help me!!
16 replies to this topic
#1
Posted 14 May 2011 - 01:01 AM
|
|
|
#2
Posted 14 May 2011 - 09:34 AM
You are going to have to be a lot more descriptive than that. What isn't working? What error do you get? Is it a runtime or compile time error? Can you post a *small* section of code that reproduces the error? What tells you that "it isn't working"?
#3
Posted 14 May 2011 - 11:09 AM
baboulas said:
I have this array: public string[,] computer = new string[10, 13];
and i want to put values in the array like this:
computer[1, 1] = Console.ReadLine();
and it doesnt work for me! please help me!!
and i want to put values in the array like this:
computer[1, 1] = Console.ReadLine();
and it doesnt work for me! please help me!!
What exactly are you intentions? the array you have created is a 2D array each element of which is a string? That makes it a total of 3 dimensions. Is that what you required? Or were you trying to only store an array of strings?
#4
Posted 16 May 2011 - 03:05 AM
fayyazlodhi said:
What exactly are you intentions? the array you have created is a 2D array each element of which is a string? That makes it a total of 3 dimensions. Is that what you required? Or were you trying to only store an array of strings?
#5
Posted 16 May 2011 - 03:55 AM
Hi there baboulas.
Firstly, it seems you are trying to set your array at the wrong place. it starts at [0,0], so setting [1,1] is actually the second element in the second series.
also, i think I should ask what you are trying to achieve. you have declared a string array, so as you have declared it, it will be a set of strings [10,13] . that means it will be 10 ITEMS wide and 13 ITEMS deep. By ITEMS I mean not characters, but strings.
Your array may look like this:
[0,0] - {1-One}
[0,1] - {1-Two}
[0,1] - {1-Three}
------------------
[0,11] - {1-Twelve}
[0,12] - {1-Thirteen}
Then the next set will be
[1,0] - {2-One}
[1,1] - {2-Two}
etc.......
So all in all your array will contain 130 elements (130 strings...like "John", "jack", "Fred" etc......
To make sure you populate your array, but making a loop that requests input from the user, a total of 130 times, and saves the values to the array sequentially.
@Mozana. The reason why fayyazlodhi, says its a 3D array, is because a string is in fact a character array, so each array item also has a few character values (like "computer[2,4][12]" will give you the 12th character of the 4th item in the second set...)
If that makes any sense.
Let me know if you get stuck
Savage
PS Read my blog:
Insane Ramblings of a developer
Firstly, it seems you are trying to set your array at the wrong place. it starts at [0,0], so setting [1,1] is actually the second element in the second series.
also, i think I should ask what you are trying to achieve. you have declared a string array, so as you have declared it, it will be a set of strings [10,13] . that means it will be 10 ITEMS wide and 13 ITEMS deep. By ITEMS I mean not characters, but strings.
Your array may look like this:
[0,0] - {1-One}
[0,1] - {1-Two}
[0,1] - {1-Three}
------------------
[0,11] - {1-Twelve}
[0,12] - {1-Thirteen}
Then the next set will be
[1,0] - {2-One}
[1,1] - {2-Two}
etc.......
So all in all your array will contain 130 elements (130 strings...like "John", "jack", "Fred" etc......
To make sure you populate your array, but making a loop that requests input from the user, a total of 130 times, and saves the values to the array sequentially.
@Mozana. The reason why fayyazlodhi, says its a 3D array, is because a string is in fact a character array, so each array item also has a few character values (like "computer[2,4][12]" will give you the 12th character of the 4th item in the second set...)
If that makes any sense.
Let me know if you get stuck
Savage
PS Read my blog:
Insane Ramblings of a developer
#6
Posted 16 May 2011 - 08:23 AM
Mozana said:
I dont Understand how that makes it a 3 dim array please explain to me, and babolous I think you can get help if maybe you tel us your intentions and the kind or error or exception.
#7
Posted 16 May 2011 - 09:37 AM
it doesnt show error it just doesnt save the input in the array, i just want 14 characteristics of a product to be given and saved in an array so i can see them later if i want.
#8
Posted 16 May 2011 - 10:43 AM
baboulas said:
it doesnt show error it just doesnt save the input in the array, i just want 14 characteristics of a product to be given and saved in an array so i can see them later if i want.
Momerath portrayed the picture correctly. I am a c programmer and like to think that way :). But irrespective of C or OO, my point was to ensure that you know string in c# has it's own memory to store characters and you ONLY declare a 2D array of strings when you actually need that.
I have seen many people coming from c thinking a string is still a character so they need a 2D array to store things such as {"book", "chair", "table"}. Where as in c# this only requires a 1D array of strings.
And i feel my hunch was right - you mentioned you just want 14 characteristics of a product to be saved in array. Why do you need 2D array to do this?
Shouldn't it be conceptually?
characterisitic[1], characterisitic[2], characterisitic[3] .... characterisitic[14]
2D will only make sense if you have MULTIPLE products though it doesn't sound like that.
You need to paste your code here in order to get us to help you out. At least we can fix your "just doesn't save the input in array issue" without much time.
#9
Posted 17 May 2011 - 12:46 AM
I need to save the 14 characteristics 10 times so i'll have 10 different products with 14 characteristics each!
#10
Posted 17 May 2011 - 01:26 AM
and here is the code:
int i;
public string[,] computer = new string[10, 13];
public void SetDesktop()
{
i = i + 1;
Console.Write("characterisitic: ");
computer[i, 0] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 1] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 2] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 3] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 4] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 5] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 6] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 7] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 8] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 9] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 10] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 11] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 12] = Console.ReadLine();
Console.WriteLine("some text...");
}
and i need to save 10 different products so i need 2d array, but without an error it doesnt save anything
int i;
public string[,] computer = new string[10, 13];
public void SetDesktop()
{
i = i + 1;
Console.Write("characterisitic: ");
computer[i, 0] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 1] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 2] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 3] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 4] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 5] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 6] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 7] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 8] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 9] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 10] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 11] = Console.ReadLine();
Console.Write("characterisitic: ");
computer[i, 12] = Console.ReadLine();
Console.WriteLine("some text...");
}
and i need to save 10 different products so i need 2d array, but without an error it doesnt save anything
#11
Posted 17 May 2011 - 05:24 AM
You are just saying: "Console.Write("characterisitic: ");"
You need to say: "Console.Write("characterisitic: " + computer[i, 0].ToString() );"
And you can write it all in only a few lines:
I made it 1x3 so that you dont have to populate it with 130 items.
Try that..
Regards
Savageflash
You need to say: "Console.Write("characterisitic: " + computer[i, 0].ToString() );"
And you can write it all in only a few lines:
string[,] computer = new string[1, 3];
for (int x = 0; x < 1; x++)
{
for (int y = 0; y < 3; y++)
{
computer[x, y] = Console.ReadLine();
Console.Write("Product " + x.ToString() + " - characterisitic " + y.ToString() + ": " + computer[x, y].ToString() + "\n");
}
}
// then if you want to display it again later:
for (int a = 0; a < 1; a++)
{
for (int b = 0; b < 3; b++)
{
Console.Write("Product " + a.ToString() + " - characterisitic " + b.ToString() + ": " + computer[a, b].ToString() + "\n");
}
}
I made it 1x3 so that you dont have to populate it with 130 items.
Try that..
Regards
Savageflash
Edited by savageflash, 17 May 2011 - 06:01 AM.
#12
Posted 17 May 2011 - 09:53 AM
i am wondering where did you initialize
variable i. Are we assuming it is zero from the start?
variable i. Are we assuming it is zero from the start?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









