0

This is what my text file looks like (example)

28:Toy
1:Chocolate bar
10:Water bottle

I want it to only extract the numbers, then add the numbers together and put it into a label or something I've tried

foreach(int number in "money.txt"){
    int sum = number + number;
    label5.Text = sum.ToString();
}

But that doesn't seem to work.

Mayank Patel
  • 8,088
  • 5
  • 55
  • 75
  • Does this answer your question? [What's the fastest way to read a text file line-by-line?](https://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) – Heretic Monkey Dec 25 '19 at 18:41

3 Answers3

0
var summ = 0;
var file = File.ReadAllLines("file.txt");
foreach (var line in file)
    {
        var number = int.Parse(line.Split(":")[0]);
        summ += number;
    }
    label5.Text = summ.ToString();

Something like this can be base, which you can use for your needs.

Ukrainis
  • 534
  • 2
  • 16
  • Getting error "Cannot convert char to string" When I click on it, it brings me to the foreach keyword. – leonlejunchen Dec 25 '19 at 18:11
  • I created a simple app to try it. This code worked for me. Variable summ was the same, as the summ of numbers in file. – Ukrainis Dec 25 '19 at 20:08
0

This is a simpler solution.

  1. Read in the file
  2. Parse the first element of the string (split with :)
  3. Sum it up.
    var sumOfAllInts = File.ReadAllLines(textFile).ToList().Select(x => int.Parse(x.Split(':').First())).Sum();
JohnyL
  • 6,894
  • 3
  • 22
  • 41
Jawad
  • 11,028
  • 3
  • 24
  • 37
0

VERY IMPORTANT !

Your code:

foreach(int number in "money.txt"){
    int sum = number + number; // BIG ERROR
    label5.Text = sum.ToString();
}

You have make a big mistake with int sum = number + number;.

Take sum to out of your scope (maybe global) first.

You have to do 2 steps:

Try this online here

Step 1: Read content of your file:

using System.IO;

// Your code
static string yourTextFileContent = File.ReadAllText("money.txt");

Step 2: I wrote a method to do your work:

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;

public class Program
{
    // Demo string
    static string yourMoney = @"28:Toy
1:Chocolate bar
10:Water bottle";
    // Your real string
    //static string yourTextFileContent = File.ReadAllText("money.txt");
    public static void Main()
    {
        Console.WriteLine("Using IEnumerable");
        // int sum = 0;
        foreach (int item in GetEnumerableIntFromString(yourMoney))
        {
            sum += item;
            Console.WriteLine(item);
        }
        Console.WriteLine($"Sum: {sum}");
        //label5.Text = sum.ToString();
    }

    // Using IEnumerable
    private static IEnumerable<int> GetEnumerableIntFromString(string str)
    {
        string[] splitLine = str.Split(new[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
        foreach(string item in splitLine)
        {
            string[] splitToGetNumber = item.Split(new[]{':'},StringSplitOptions.RemoveEmptyEntries);
            yield return Convert.ToInt32(splitToGetNumber[0].Trim());
        }
    }
}

If you have an Enumerable, you want to Convert to List:

Enumerable<int> enumerable = yourEnumerable;
List<int> asList = enumerable.ToList();
Nguyen Van Thanh
  • 805
  • 9
  • 18