0

I'm randomly generating simple math equations and need to get the numbers from the string and convert them into integers so I can add them. However, when I run the program I receive the "input string was not in the correct format" error on the "int N1Q1" line.

Is there something I'm missing? Would there be a better way to extract and convert a number from a string?

Question1.Text = Convert.ToString(random.Next(1, 9) + " + " + random.Next(1, 9) + " = ");

string FirstQuestion = Convert.ToString(Question1.Text);

int N1Q1 = Convert.ToInt32(FirstQuestion.Substring(0,1));
int N2Q1 = Convert.ToInt32(FirstQuestion.Substring(5,1));
5
  • 2
    Yes, do not do this in the first place. Store your numbers as numbers and not in a string. The string is good for presentation (showing the user). In your case you could extend Question with additional properties of type int and do the assignment there. Alternatively you can create a wraper and encapsulate the Question type and enrich it with additional members. Commented May 9, 2019 at 21:08
  • Cause FirstQuestion.Substring(0,1) isn't an Int32 type at all Commented May 9, 2019 at 21:08
  • 2
    Question1.Text is already a string, why are you calling Convert.ToString on it? Commented May 9, 2019 at 21:08
  • Igor is right your approach is questionable as a whole. But you could just output the values of your Substring()s or look at them in the debugger to see if they're valid integer representations. I'd guess your FirstQuestion.Substring(5,1) is actually a space... Commented May 9, 2019 at 21:13
  • The compiler will automatically convert a number to a string if you concatenate it with one, so you don't need the Convert.ToString in the first line of code, and the Text property is already a string, so you don't need it in the second line either. Commented May 9, 2019 at 21:16

3 Answers 3

2

Here's a different way to go about it, you could make each random.Next() call into its own variable, then you wont have to do the conversions. Something like this:

int random1 = random.Next(1,9);
int random2 = random.Next(1,9);
Question1.Text = $"{random1} + {random2} = ";

You also don't need to convert Question1.Text to a string, because it is already a string. Also, using this method, you already have the random numbers captured as variables, then you wont have to convert them back into integers

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

Comments

0

Looks like you are off by one in your substring in N2Q1:

int N2Q1 = Convert.ToInt32(FirstQuestion.Substring(4, 1));

Comments

0

do not forget to declare at the first place the random as the following

Random random = new Random();

second, in N2Q1 you calculated wrong, it should be as the following:

int N2Q1 = Convert.ToInt32(FirstQuestion.Substring(4, 1));

Comments

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.