jimjam456
jimjam456

Reputation: 91

Input string was not in correct format C#

I'm currently writing a program that takes a persons name and 5 variables. Then with those 5 variables I'm tasked with finding the avg/sample variance. My current code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApplication1
    {
        class Program
        {
        static void Main(string[] args)
        {
            string userName = "";
            string variables = "";
            int[] vars = parseVariableString(variables);
            vars = new int[5];
            int sum = 0;

            Console.Write("Please enter your name: ", userName);
            userName = Console.ReadLine();
            Console.ReadLine();
            Console.Write("Please enter 5 numbers with a space or coma inbetween: ", vars);

            for (int i = 0; i < vars.Length; i++)
            {
                int number = vars[i];
                sum += number;
            }
            float avg = sum/(float)vars.Length;
            float variance = 0;
            for (int i = 0; i < vars.Length; i++)
            {
                int number = vars[i];
                float f = number - avg;
                variance += (float)Math.Pow(f, 2);
            }
            float sv = variance / (vars.Length - 1);

            Console.Write(" Your name is: ", userName);
            Console.ReadLine();
            Console.Write("The average of your numbers is: ", avg);
            Console.ReadLine();
            Console.Write("The sample variance of your numbers is: ", sv);
            Console.ReadKey();
        }

        private static int[] parseVariableString(String variables)
        {
            String[] varArray = variables.Split(' ', ',');
            int[] intArray = new int[varArray.Length];

            for (int i = 0; i < varArray.Length; i++)
            {
                String variable = varArray[i];
                int integer = Convert.ToInt32(variable);
                intArray[i] = integer;
            }
            return intArray;
        }
    }
}

I'm getting the

Input string was not in correct format

error at int integer = Convert.ToInt32(variable);. I'm not understanding why exactly I'm getting this error. I looked online for what it means, a lot of people say to use an int.parse but from what I read you get this because the variable doesn't recognize that there is a value associated with it. Any help would be greatly appreciated.

Upvotes: 1

Views: 6815

Answers (1)

PiousVenom
PiousVenom

Reputation: 6908

    string variables = "";
    int[] vars = parseVariableString(variables);

You're declaring an empty string, and then trying to convert that string into an int.

private static int[] parseVariableString(String variables)
{
    String[] varArray = variables.Split(' ', ',');
    int[] intArray = new int[varArray.Length];

    for (int i = 0; i < varArray.Length; i++)
    {
        String variable = varArray[i];
        int integer = Convert.ToInt32(variable);
        intArray[i] = integer;
    }
    return intArray;
}

As well as when you're passing in an empty string, there's nothing to split, and thus your array is empty.

Upvotes: 6

Related Questions