1

I encounter a error when I try to convert a string variable which is read from a text file to int. How can I convert the number that is read from a text to int?

namespace ConsoleApp1
{
    class FileWriter
    {
        static void Main()
        {
            int a = 6;
            StreamWriter writer = new StreamWriter(@"D:\asd.txt");
            using (writer)
            {
               writer.WriteLine(a);
            }
        }
    }
}
class FileReader
{
     static void Second()
     {
        StreamReader reader = new StreamReader(@"D:\asd.txt");
        using (reader)
        {

            string line = reader.ReadLine();
            Console.WriteLine(line);

           }

            );

    }

    }
Smily
  • 1,745
  • 2
  • 7
  • 21
Noyadean
  • 13
  • 3
  • 2
    Upload also the error you are getting. In the future, it helps people (us) understand, what is the problem. In this context, format of the file is important too. So maybe try to update you question. – Vojtěch Mráz Jan 28 '20 at 11:17
  • Please edit your code so that it is readable and compilable – th33lf Jan 28 '20 at 11:18
  • yes and also try to write proper question as it is not that clear – Logica Jan 28 '20 at 11:18
  • 1
    Does this answer your question? [How can I convert String to Int?](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – Lukasz Szczygielek Jan 28 '20 at 11:19
  • `if (int.TryParse(line, out int a)) {/*line is a valid integer which is parsed into a*/} else {/*line doesn't represent integer*/}` – Dmitry Bychenko Jan 28 '20 at 11:20
  • 1
    Review note: initialize `reader` and `writer` as part of the `using` block, not before it. – madreflection Jan 28 '20 at 11:21
  • 2
    [There](https://stackoverflow.com/q/1019793/2716623) [is](https://stackoverflow.com/q/21663357/2716623) [so](https://stackoverflow.com/q/2344411/2716623) [many](https://stackoverflow.com/q/42001743/2716623) [duplicates](https://stackoverflow.com/q/45523185/2716623) [on](https://stackoverflow.com/q/16790337/2716623) [stackoverflow](https://stackoverflow.com/q/37814186/2716623) :( – vasily.sib Jan 28 '20 at 11:22

3 Answers3

3

You use Int32.TryParse it's safe

ilyes
  • 372
  • 1
  • 9
2

You can try something like this, with TryParse and Trim() to remove any whitespaces.

class FileReader
{
    static void Second()
    {
        StreamReader reader = new StreamReader(@"D:\asd.txt");

        using(reader)
        {
            int number;
            string line = reader.ReadLine();
            bool success = Int32.TryParse(value.Trim(), out number);
            if (success) {
                Console.WriteLine("Number is:" + number);
            } else {
                Console.WriteLine("Could not parse the number");
            }
        }
    }
}
Athanasios Kataras
  • 20,791
  • 3
  • 24
  • 45
1

You can use the Int32.TryParse as mention in below code.

 int no = 0;
 string number = "6";
 Int32.TryParse(number, out no);
Hardik Sanghavi
  • 232
  • 2
  • 6