I have a text file that I am reading that looks like this:
"2002", "1", "500",
"2003", "1", "501",
"2001", "1", "500",
"2000", "1", "503",
"2001", "2", "500",
"2000", "2", "540",
"2003", "2", "550",
"2002", "2", "523",
The first column is 'year', the second is 'internal/external (1 means internal, 2 means external)', and the third is 'amount'.
I want to use these numbers to make a report that looks like the following:
Year: Internal External
2002 500 523
2003 501 550
2001 500 500
2000 503 540
There is no particular order that these values have to appear in. However, the year must match the 'amount' values. Also, if the middle value is '1' it will be put under 'internal' and if the value is '2' it will be put under 'external'. I have already created the code to read from the file. I currently have all these values stored in three parallel arrays (int[] year, int[] inex, and int[] amount). The output will be displayed within a Listbox. I have been trying to find a solution for the past 2 days, without any luck.
All help is appreciated.

How in the world?
paragp
With your array you have differerent values for the same years:
It seems your data will be incorrect.
If 2000 has 321 in Internal in one field of the array....how can it have a different value in Internal in another field with the same year Internal will have 2 different values for the same year...
It seems that you need to rename the fields; dogfood internal and catfood internal, dogfood external and catfood external.
Teymur Hajiyev
String[] fields = new String
;
fields[0] = "2000 1 501";
fields[1] = "2000 2 502";
fields[2] = "2001 2 602";
fields[3] = "2001 1 601";
fields[4] = "2002 2 702";
fields[5] = "2002 1 701";
for (int i = 0; i < fields.Length; i++)
{
string year = fields
.Substring(0, 4);
for (int ii = 0; ii < fields.Length; ii++)
{
string internalvalue = string.Empty;
string externalvalue = string.Empty;
bool noAdd = false;
if (year == fields[ii].Substring(0, 4) && fields[ii] != fields
)
{
if (fields[ii].Substring(5, 1) == "1")
{
Console.WriteLine(fields[ii].Substring(6));
internalvalue = fields
.Substring(6);
externalvalue = fields[ii].Substring(6);
noAdd = true;
}
else
{
noAdd = false;
Console.WriteLine(fields[ii].Substring(6));
internalvalue = fields
.Substring(6);
externalvalue = fields[ii].Substring(6);
}
if (!noAdd)
{
listBox1.Items.Add(year + internalvalue + externalvalue);
}
}
}
}
}
DotNet_369
Well, if I have the following array:
String[] fields = new String[16];
fields[0] = "2000 1 500"
fields[1] = "2000 1 322"
fields[2] = "2000 2 200"
fields[3] = "2000 2 486"
fields[4] = "2001 1 100"
fields[5] = "2001 1 500"
fields = "2001 2 340"
fields[7] = "2001 2 400"
fields = "2002 1 300"
fields[9] = "2002 1 400"
fields[10] = "2002 2 200"
fields[11] = "2002 2 999"
fields[12] = "2003 1 230"
fields[13] = "2003 1 400"
fields[14] = "2003 2 500"
fields[15] = "2003 2 340"
I am hoping for this output:
Year Internal External
2000 500 200
2000 322 486
2001 100 340
2001 500 400
2002 300 200
2002 400 999
2003 230 500
2003 400 340
Fidencio Monroy
Thanks cablehead, I'm going to try out this code... could you please define what the lightbulbs are really Silly emoticons.
Thanks
Pirgher
A "simple" way would be to create a Hashtable with the Year as the key and a structure that contains two strings (InternalAmount and ExternalAmount).
As you process the file, you pull out the year which is the key and then process the next value as well as the amount.
string key = "2002"; // actually from the file
string internalAmount = null;
string externalAmount = null;
if (column2 == "1") internalAmount = "500"; // from file
AmountStructure struct = (AmountStructure)hashtable[key];
if (column2 == "1") struct.InternalAmount = "500"; // from file
etc...
To handle the ListBox, move the Hashtable over to an ArrayList (which will contain AmountStructure's).
In the AmountStructure overrride ToString to display your data "2001 500 500".
Then bind the ArrayList to the listbox datasource.
Tomay
Karsinogeeni
It looks like it should be:
fields
didn't save the code....
the bulb is brackets i.
Raymond Chen
Looooooka
tomkle
jerrymei
k1dfreeze
Shula
String
[] fields = new Stringfields[0] =
"2000 1 501";fields[1] =
"2000 2 502";fields[2] =
"2001 2 602";fields[3] =
"2001 1 601";fields[4] =
"2002 2 702";fields[5] =
"2002 1 701";fields
=
"2003 2 802";fields[7] =
"2003 1 801"; for (int i = 0; i < fields.Length; i++){
string year = fields{
string internalvalue = string.Empty; string externalvalue = string.Empty; bool noAdd = false; if (year == fields[ii].Substring(0, 4) && fields[ii] != fields{
if (fields[ii].Substring(5, 1) == "1"){
noAdd =
true;}
else{
noAdd =
false; Console.WriteLine(fields[ii].Substring(6));internalvalue = fields
.Substring(6);
externalvalue = fields[ii].Substring(6);
}
if (!noAdd){
listBox1.Items.Add(year + internalvalue + externalvalue);
}
}
}
}
lucasjordan
This doesn't seem to be working with my array. I get a very long output within my listbox that doesn't seem to be correct.
I have the following code:
String[] fields = new String[16];
fields[0] = "2000 1 500"
= "2001 2 340"
= "2002 1 300"
fields[1] = "2000 1 322"
fields[2] = "2000 2 200"
fields[3] = "2000 2 486"
fields[4] = "2001 1 100"
fields[5] = "2001 1 500"
fields
fields[7] = "2001 2 400"
fields
fields[9] = "2002 1 400"
fields[10] = "2002 2 200"
fields[11] = "2002 2 999"
fields[12] = "2003 1 230"
fields[13] = "2003 1 400"
fields[14] = "2003 2 500"
fields[15] = "2003 2 340"
for
(int i = 0; i < fields.Length; i++){
string year = fields{
string internalvalue = string.Empty; string externalvalue = string.Empty; bool noAdd = false; if (year == fields[ii].Substring(0, 4) && fields[ii] != fields{
if (fields[ii].Substring(5, 1) == "1"){
noAdd =
true;}
else{
noAdd =
false;Console.WriteLine(fields[ii].Substring(6));
internalvalue = fields
.Substring(6);
externalvalue = fields[ii].Substring(6);
}
if (!noAdd){
listBox1.Items.Add(year + internalvalue + externalvalue);
}
}
}
}
}