user2301717
user2301717

Reputation: 1169

C# - Input string was not in a correct format

I am working on a simple windows forms application that the user enters a string with delimiters and I parse the string and only get the variables out of the string. So for example if the user enters:

2X + 5Y + z^3

I extract the values 2,5 and 3 from the "equation" and simply add them together.

This is how I get the integer values from a string.

int thirdValue
string temp;
temp = Regex.Match(variables[3], @"\d+").Value
thirdValue = int.Parse(temp);

variables is just an array of strings I use to store strings after parsing.

However, I get the following error when I run the application:

Input string was not in a correct format

Upvotes: 5

Views: 3038

Answers (4)

astef
astef

Reputation: 9478

I prefer simple and lightweight solutions without Regex:

static class Program
{
    static void Main()
    {
        Console.WriteLine("2X + 65Y + z^3".GetNumbersFromString().Sum());
        Console.ReadLine();
    }

    static IEnumerable<int> GetNumbersFromString(this string input)
    {
        StringBuilder number = new StringBuilder();
        foreach (char ch in input)
        {
            if (char.IsDigit(ch))
                number.Append(ch);                
            else if (number.Length > 0)
            {
                yield return int.Parse(number.ToString());
                number.Clear();
            }
        }
        yield return int.Parse(number.ToString());
    }
}

Upvotes: 1

Mr Heelis
Mr Heelis

Reputation: 2546

Why i everyone moaning about this question and marking it down? it's incredibly easy to explain what is happening and the questioner was right to say it as he did. There is nothing wrong whatsoever.

Regex.Match(variables[3], @"\d+").Value

throws a Input string was not in a correct format.. FormatException if the string (here it's variables[3]) doesn't contain any numbers. It also does it if it can't access variables[3] within the memory stack of an Array when running as a service. I SUSPECT THIS IS A BUG The error is that the .Value is empty and the .Match failed.

Now quite honestly this is a feature masquerading as a bug if you ask me, but it's meant to be a design feature. The right way (IMHO) to have done this method would be to return a blank string. But they don't they throw a FormatException. Go figure. It is for this reason you were advised by astef to not even bother with Regex because it throws exceptions and is confusing. But he got marked down too!

The way round it is to use this simple additional method they also made

if (Regex.IsMatch(variables[3], @"\d+")){
   temp = Regex.Match(variables[3], @"\d+").Value
}

If this still doesn't work for you you cannot use Regex for this. I have seen in a c# service that this doesn't work and throws incorrect errors. So I had to stop using Regex

Upvotes: 4

Himanshu Kumar
Himanshu Kumar

Reputation: 571

This should solve your problem:

string temp;
temp = Regex.Matches(textBox1.Text, @"\d+", RegexOptions.IgnoreCase)[2].Value;
int thirdValue = int.Parse(temp);

Upvotes: 0

TimD
TimD

Reputation: 1

you can change the string to char array and check if its a digit and count them up.

        string temp = textBox1.Text;
        char[] arra = temp.ToCharArray();
        int total = 0;
        foreach (char t in arra)
        {
            if (char.IsDigit(t))
            {
                total += int.Parse(t + "");
            }
        }
        textBox1.Text = total.ToString();

Upvotes: 0

Related Questions