How in the world?

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. 



Answer this question

How in the world?

  • Dark Otter

    Sorry...my code assumed only 2 instances of any particular year. Internal and External.

  • CBueche

    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.



  • mangs_22

    Hmm... seems this problem is going to be harder to solve than I thought.
  • spectralvortex

    Can you add all the values of all of 2000 Internal values That way you would end up with one Internal value for the year.

  • MMike

    Thanks cablehead, I'm going to try out this code... could you please define what the lightbulbs are really Silly emoticons.

    Thanks


  • kapoorg

    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.



  • Luis Maria Guayan

    Is that the goal (To have one value for internal and one for external) for each year...

  • nnnnnpatel

    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


  • alikashani

    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"
    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"
    fieldsDevil = "2001 2 340"
    fields[7] = "2001 2 400"
    fieldsMusic = "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"

    for (int i = 0; i < fields.Length; i++)

    {

    string year = fieldsIdea.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] != fieldsIdea)

    {

    if (fields[ii].Substring(5, 1) == "1")

    {

    noAdd = true;

    }

    else

    {

    noAdd = false;

    Console.WriteLine(fields[ii].Substring(6));

    internalvalue = fieldsIdea.Substring(6);

    externalvalue = fields[ii].Substring(6);

    }

    if (!noAdd)

    {

    listBox1.Items.Add(year + internalvalue + externalvalue);

    }

    }

    }

    }

    }


  • jabdulius

    Unfortunately not...I'm stumped.
  • maila

    So there is no way that I am able to modify the code to fit my array
  • Richard Robertson 76

    String[] fields = new StringMusic;

    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";

    fieldsDevil = "2003 2 802";

    fields[7] = "2003 1 801";

    for (int i = 0; i < fields.Length; i++)

    {

    string year = fieldsIdea.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] != fieldsIdea)

    {

    if (fields[ii].Substring(5, 1) == "1")

    {

    noAdd = true;

    }

    else

    {

    noAdd = false;

    Console.WriteLine(fields[ii].Substring(6));

    internalvalue = fieldsIdea.Substring(6);

    externalvalue = fields[ii].Substring(6);

    }

    if (!noAdd)

    {

    listBox1.Items.Add(year + internalvalue + externalvalue);

    }

    }

    }

    }



  • wtfskh

    String[] fields = new StringDevil;

    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 = fieldsIdea.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] != fieldsIdea)

    {

    if (fields[ii].Substring(5, 1) == "1")

    {

    Console.WriteLine(fields[ii].Substring(6));

    internalvalue = fieldsIdea.Substring(6);

    externalvalue = fields[ii].Substring(6);

    noAdd = true;

    }

    else

    {

    noAdd = false;

    Console.WriteLine(fields[ii].Substring(6));

    internalvalue = fieldsIdea.Substring(6);

    externalvalue = fields[ii].Substring(6);

    }

    if (!noAdd)

    {

    listBox1.Items.Add(year + internalvalue + externalvalue);

    }

    }

    }

    }

    }



  • WhatEverCode

    It looks like it should be:

    fieldsIdea  

     

    didn't save the code....

     

    the bulb is brackets i.



  • How in the world?