1

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.

5
  • 2
    It means that it was not just numbers and that it cannot convert it to an int. Commented Mar 1, 2013 at 18:32
  • what is the value of variable when the exception is thrown? Commented Mar 1, 2013 at 18:33
  • Better use TryParse method, it will return the bool value indicating whether the string could be parsed (converted to int32) or not. Commented Mar 1, 2013 at 18:33
  • @vlad How does one view the value of the variable? I'm looking at the error message but not sure where to find the information. Commented Mar 1, 2013 at 18:36
  • @jimjam456 assuming you're using Visual Studio, hover over variable during debugging or add it to a watch list. Commented Mar 1, 2013 at 18:41

1 Answer 1

6
    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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh okay, that makes sense. I didn't realize it was empty. So I'm assuming then the current flow isn't right. I figured that when ran the user would be asked for their name and the numbers which would then fill the array.
@jimjam456 You might want to mark the post as an accepted answer if it was helpful to you and it helped you resolve your problem. You will have to click the check mark below the voting buttons to do the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.