none
C# Console Application - How to convert a number in a text file to an integer with ReadAllLines? RRS feed

  • Question

  • I have a lot of text files with one number in each of them. I attached one of the files (which is named "Balance.txt") to a string called "loginBalancePath" and then I created the variable "readLoginBalance" and used the following code:
    var readLoginBalance = File.ReadAllLines(loginBalancePath);

    There is a number in the file so the thing I wanted to do is to convert the number that is in the file to an integer so I'll be able to use it in some kind of a bank account management and math exercises. Does that even possible? And if it does, what do I need to write?
    Wednesday, May 2, 2018 1:25 PM

Answers

All replies

  • var readLoginBalance = File.ReadAllLines(loginBalancePath);
    int Balance;

    if (int.TryParse(readLoginBalance[0], out Balance)) { Console.WriteLine(Balance); } else { Console.WriteLine("Balance Conversion to Int Failed."); }

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number

    
    




    • Edited by Vikram Manjare Wednesday, May 2, 2018 1:41 PM
    • Marked as answer by yuvalmus Wednesday, May 2, 2018 3:09 PM
    Wednesday, May 2, 2018 1:40 PM
  • Hello,

    I would do your work in a class as follows. Note the TryParse uses C# 7 out var feature.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            var ops = new Operations();
            var values = ops.GetData();
            values.ForEach(item => Console.WriteLine(item));
            Console.WriteLine(values.Sum());
        }
    }
    
    public class Operations
    {
        public List<int> GetData()
        {
            List<int> results = new List<int>();
    
            var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,"*.txt");
            var fileData = "";
    
            foreach (var file in files)
            {
                fileData = File.ReadAllText(file).Trim();
                if (int.TryParse(fileData, out var value))
                {
                    results.Add(value);
                }
            }
    
            return results;
        }
    }

    Depending on how many files there are consider using async if there are a lot of files to keep the app responsive.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 1:56 PM
    Moderator
  • If the file contains a single line and that single line contains a number then ReadAllLines is overkill. Just use ReadAllText.

    //You mention money so you should be using a decimal, not int
    if (Decimal.TryParse(File.ReadAllText(loginBalancePath), out decimal balance))
       //balance has the value


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 2, 2018 1:59 PM
    Moderator