简体   繁体   中英

c# split integers and strings from text file and add them to two different listbox

I'm trying to parse sales information from text file and put them to two listboxes

The text file contains this information:

Sam West $10,000.00
Mae West $125,900.00
North West $2,000.00
Michelle Smith $25,000.00
John Smith $12,500.00
Martin Smith $19,900.00
David Sampson $32,500.00
Joan Sampson $5,990.00
Sam Sampson $10,000.00
Mae Sampson $125,500.00
North Sampson $2,000.00
Michelle West $25,000.00
John Johnson $12,500.00
Martin Johnson $19,900.00
David Johnson $32,500.00
Joan Johnson $5,990.00
Sam Hartmann $10,000.00
Mae Hartmann $125,100.00
North Hartmann $2,000.00
Michelle Hartmann $25,000.00
John Johnson $12,500.00
Martin Hartmann $19,900.00
David Hartmann $32,500.00
Joan Hartmann $5,990.00

and my code is here

private void btnReadInSalesData_Click(object sender, EventArgs e)
{
    StreamReader reader = new StreamReader("SalesNumbers.txt");
    List<int> numbers = new List<int>();
    int intTotal = 0;

    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        string[] tokens = line.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string s in tokens)
        {
            if (int.TryParse(s, out intTotal))
                numbers.Add(intTotal);
            lstTotalSales.Items.Add(s);
        }
    }

And here is a picture of the output http://s24.postimg.org/ylm8vl9at/output.jpg

Simply I want to read the text file and add the total sales to lstTotalSales listbox and add The Full name to lstNames list box.

Thank you

You should use decimal instead of int , because your numbers are not integers.

You should also change your logic within the loop. I think it should be more like that:

foreach (string s in tokens)
{
    if (decimal.TryParse(s, out decTotal))
    {
        numbers.Add(decTotal);
        lstTotalSales.Items.Add(s);
    }
    else
    {
        lstNames.Items.Add(s);
    }
}

You missed to split each line into name and value.

Try this:

        string[] lines = File.ReadAllLines("SalesNumbers.txt");
        foreach (string line in lines) {
            string[] s = line.Split("$".ToCharArray());
            if (s.Length<2) { /* */ }
            double d;
            if (!double.TryParse(s[1], NumberStyles.Float, CultureInfo.CurrentCulture, out d)) {                
                // Handle if not a number
            }
            lstNames.Items.Add(s[0]);
            lstTotalSales.Items.Add(d);
        }

EDIT

While we convert the value to a double, you have to add the currency symbol when displaying the value:

string text = "$" + value.ToString();

Please note that we imply always splitting with the $ symbol.

Assuming multiple currency symbols, you have to keep track of the split symbol:

string currencySymbols = "$€Y";
//...
int index = line.IndexOfAny(currencySymbols.ToCharArray());
if (index<0) {} //Nothing found
char usedSymbol = line[index];            // <- found symbol
string name = line.Substring(0, index);
string value = line.Substring(index+1);
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM