none
error: Cannot implicitly convert type string to int RRS feed

  • Question

  • Hi guys, my code seems to be working except for when I try to get the total sum of the number of the numbers which are read from a text file. The program that I am trying to develop is a Random Number File Reader program. I get this error: Cannot implicitly convert type 'string' to 'int'. I declared my total as an int. Should I convert it to a string also?

    namespace Programming_problem_5._14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnRead_Click(object sender, EventArgs e)
            {
                try
                {
                    // Declare a StreamReader variable.
                    StreamReader inputFile;            
    
                    // Declare a variable to hold the random numbers.
                    string amount;
    
                    // Declare a variable for the number of random numbers read from the text File.
                    int linecount = 0;
    
                    // Declare variable for the total sum of the random numbers.
                    int total = 0;
                    
                    // Create output file.
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        // Open the selected file.
                        inputFile = File.OpenText(openFileDialog1.FileName);
    
                        // Read the File's contents.
                        while (!inputFile.EndOfStream)
                        {
                            // Get the amounts
                            amount = inputFile.ReadLine();
    
                            // Add the amounts to the ListBox.
                            lstNumbers.Items.Add(amount);
    
                            // Increment for the number of random numbers in text file.
                            linecount++;
    
                            // Increment for the total sum of the random numbers read from the text File.
                           total += amount;
                        }
                        // Display the total sum of the random numbers read from the text File.
                        lstNumbers.Items.Add("The total of the numbers is: " + total);
    
                        // Display the number of random numbers read from text file.
                        lstNumbers.Items.Add("The number of random numbers read from the File is "+ linecount);
                     
                        // Close the file
                        inputFile.Close();
                    }
                }
                catch (Exception ex)
                {
                    // Display an error message.
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    

    Tuesday, September 4, 2018 8:01 AM

All replies

  • string and int are obviously two different types of data.

    strings hold text values such as "hello world"

    ints hold whole numbers such as 12345.

    Important thing to realise is that even if you have a string variable with the value "12345", this is not a number as far as a computer is concerned: It is still a string that holds the character '1' followed by the character '2' and so on.

    If you have a string that you know does hold a textual representation of a number then you must explicitly convert.

    int myNumber;
    
    if (int.TryParse(amount, out myNumber))
    {
       // conversion worked
       total += myNumber;
    }
    else
    {
       // conversion did NOT work. i.e. the string amount held some value that could not be converted into a number.
       // Deal with the problem here...
    }

    Notice that you should allow for the possibility that your string variable holds text that can't actually be converted into a number (imagine if somebody picked a file that just contained text rather than the list of numbers that you expected).

    Similarly if you have a int variable and want to convert back to a string you can do:

    string myText = myNumber.ToString();

    • Proposed as answer by Wizend Tuesday, September 4, 2018 10:01 AM
    Tuesday, September 4, 2018 8:23 AM
  • Hi thank you for your advice I managed to get the programme to work using the logic that you explained to me. Here is the code

    namespace Programming_problem_5._14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnRead_Click(object sender, EventArgs e)
            {
                try
                {
                    // Declare a StreamReader variable.
                    StreamReader inputFile;
    
                    // Declare a variable to hold the random numbers.
                    int randomNumbers;
                    
                    // Declare a variable for the number of random numbers read from the text File.
                    int linecount = 0;
    
                    // Declare variable for the total sum of the random numbers.
                    int total = 0;
    
                    // Create output file.
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        // Open the selected file.
                        inputFile = File.OpenText(openFileDialog1.FileName);
    
                        // Read the File's contents.
                        while (!inputFile.EndOfStream)
                        {
                            // Get the random numbers
                            randomNumbers = int.Parse(inputFile.ReadLine());
    
                            // Add the random numbers to the ListBox.
                            lstNumbers.Items.Add(randomNumbers);
    
                            // Increment for the number of random numbers in text file.
                            linecount++;
    
                            // Increment for the total sum of the random numbers read from the text File.
                            total += randomNumbers;
                        }
                        // Display the total sum of the random numbers read from the text File.
                        lstNumbers.Items.Add("The total of the numbers is: " + total);
    
                        // Display the number of random numbers read from text file.
                        lstNumbers.Items.Add("The number of random numbers read from the File is "+ linecount);
                     
                        // Close the file
                        inputFile.Close();
                    }
                }
                catch (Exception ex)
                {
                    // Display an error message.
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

    This is the only change that I did  randomNumbers = int.Parse(inputFile.ReadLine()) and I changed the randomNumber variable from a string to an int

    Thank you!

    • Proposed as answer by Stanly Fan Wednesday, September 5, 2018 2:07 AM
    Tuesday, September 4, 2018 9:51 AM
  • Hi thank you for your advice I managed to get the programme to work using the logic that you explained to me. Here is the code

    namespace Programming_problem_5._14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnRead_Click(object sender, EventArgs e)
            {
                try
                {
                    // Declare a StreamReader variable.
                    StreamReader inputFile;
    
                    // Declare a variable to hold the random numbers.
                    int randomNumbers;
                    
                    // Declare a variable for the number of random numbers read from the text File.
                    int linecount = 0;
    
                    // Declare variable for the total sum of the random numbers.
                    int total = 0;
    
                    // Create output file.
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        // Open the selected file.
                        inputFile = File.OpenText(openFileDialog1.FileName);
    
                        // Read the File's contents.
                        while (!inputFile.EndOfStream)
                        {
                            // Get the random numbers
                            randomNumbers = int.Parse(inputFile.ReadLine());
    
                            // Add the random numbers to the ListBox.
                            lstNumbers.Items.Add(randomNumbers);
    
                            // Increment for the number of random numbers in text file.
                            linecount++;
    
                            // Increment for the total sum of the random numbers read from the text File.
                            total += randomNumbers;
                        }
                        // Display the total sum of the random numbers read from the text File.
                        lstNumbers.Items.Add("The total of the numbers is: " + total);
    
                        // Display the number of random numbers read from text file.
                        lstNumbers.Items.Add("The number of random numbers read from the File is "+ linecount);
                     
                        // Close the file
                        inputFile.Close();
                    }
                }
                catch (Exception ex)
                {
                    // Display an error message.
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

    This is the only change that I did  randomNumbers = int.Parse(inputFile.ReadLine()) and I changed the randomNumber variable from a string to an int

    Thank you!

    Hi kgosi,

    So I guess you have solved your problem now, if so, hope you can close this thread by marking the solution as answer as this will help others looking for the same or similar issues down the road.

    Thanks for your understanding.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 5, 2018 2:07 AM