none
input string was not in correct format; c# ; please help :) RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace TotalSales
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private double Total(int[] iArray)
            {

                int total = 0;
                double finaltotal;

                for (int index = 0; index < iArray.Length; index++)
                {
                    total += iArray[index];
                }

                finaltotal = (double)total;

                return finaltotal;
            }

            private double Average(int[] iArray)
            {
                int total = 0;
                double average;

                for (int index = 0; index < iArray.Length; index++)
                {
                    total += iArray[index];
                }

                average = (double) total / iArray.Length;

                return average;
            }

            private int Lowest(int[] iArray)
            {
                int lowest = iArray[0];

                for (int index = 1; index < iArray.Length; index++)
                {
                    if (iArray[index] < lowest)
                    {
                        lowest = iArray[index];
                    }

                }
                return lowest;
            }

            private int Highest(int[] iArray)
            {
                int highest = iArray[0];

                for (int index = 1; index < iArray.Length; index++)
                {
                    if (iArray[index] > highest)
                    {
                        highest = iArray[index];
                    }

                }
                return highest;
            }


            private void button1_Click(object sender, EventArgs e)
            {

                try
                {
                    const int SIZE = 7;
                    int[] sales = new int[SIZE];
                    int index = 0;
                    int lowestsales;
                    int highestsales;
                    double totalsales;
                    double averagesales;
                    
                    StreamReader inputFile;
                    inputFile = File.OpenText("Sales.txt");

                    while (!inputFile.EndOfStream && index < sales.Length)
                    {
                        sales[index] = int.Parse(inputFile.ReadLine());
                        index++;
                    }

                    inputFile.Close();

                    foreach (int value in sales)
                    {
                        listBox1.Items.Add(value);
                    }

                    totalsales = Total(sales);
                    averagesales = Average(sales);
                    lowestsales = Lowest(sales);
                    highestsales = Highest(sales);

                    label3.Text = totalsales.ToString();
                    label5.Text = averagesales.ToString("n1");
                    label7.Text = lowestsales.ToString();
                    label8.Text = highestsales.ToString();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

               

            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            }

        }
    Saturday, December 8, 2012 3:29 PM

Answers

  • then why don't you do double.Parse ?

    I think you are very much clear about basic data types in C# . These are double types.


    One good question is equivalent to ten best answers.

    Saturday, December 8, 2012 3:54 PM
  • Adam :

    One way of doing this..

           double [] Sales =  File.ReadAllLines(@"C:\data.txt").Select(x => double.Parse(x)).ToArray();
                Console.WriteLine("Max = {0} Min = {1}  Avg = {2}  Sum = {3}", Sales.Max(), Sales.Min(), Sales.Average(), Sales.Sum());
      

    Saturday, December 8, 2012 4:36 PM

All replies

  • i get no errors when i run the code until i click the button
    Saturday, December 8, 2012 3:35 PM
  •  while (!inputFile.EndOfStream && index < sales.Length)
                    {
                        sales[index] = int.Parse(inputFile.ReadLine());
                        index++;
                    }

    This is the possible loophole . int.Parse will throw exception when inputs string is in wrong format which can not be converted to int .

    You can use int.TryParse instead .

     while (!inputFile.EndOfStream && index < sales.Length)
                    {
    
                       int res;
    
                       bool c = int.TryParse(inputFile.ReadLine(),out res);
    
                        sales[index] = res;
                        index++;
                    } 

    If input string is in wrong format then res will get zero initialized .

    Hope this helps.


    One good question is equivalent to ten best answers.

    Saturday, December 8, 2012 3:37 PM
  • Hi,

    the error might come from this line:

    sales[index] = int.Parse(inputFile.ReadLine()); more exactly the int.Parse method.

    Check your inputfile. It may contain blank lines or lines that are not completely numeric.

    A good choice for these operations is the use of the TryParse methods of the numeric-classes.

    http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

    //untested:

                while (!inputFile.EndOfStream && index < sales.Length)
                {
                    string s = inputFile.ReadLine();
                    int i = 0;
                    if(Int32.TryParse(s, out i))
                        sales[index] = i;
                    index++;
                }

    Regards,

      Thorsten


    Saturday, December 8, 2012 3:39 PM
  • Hello, It looks following line is throwing exception

     sales[index] = int.Parse(inputFile.ReadLine());

    your input file might not have proper values

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Saturday, December 8, 2012 3:39 PM
  • it just resets to all 0's . urgh i hate it when this happens!

    adam v

    Saturday, December 8, 2012 3:40 PM
  • this is the txt file

    1245.67
    1189.55
    1098.72
    1456.88
    2109.34
    1987.55
    1872.36


    adam v

    Saturday, December 8, 2012 3:42 PM
  • then why don't you do double.Parse ?

    I think you are very much clear about basic data types in C# . These are double types.


    One good question is equivalent to ten best answers.

    Saturday, December 8, 2012 3:54 PM
  • Adam :

    One way of doing this..

           double [] Sales =  File.ReadAllLines(@"C:\data.txt").Select(x => double.Parse(x)).ToArray();
                Console.WriteLine("Max = {0} Min = {1}  Avg = {2}  Sum = {3}", Sales.Max(), Sales.Min(), Sales.Average(), Sales.Sum());
      

    Saturday, December 8, 2012 4:36 PM