0

I have this C# code here, and what I'm trying to do is that when I put a number with many digits the application crashes and I want it to not crash but I don't know what to do, I tried changing the .Parse command but Idk which command to use instead. An example is that when I run the application I want to put a number like 564984894897987878 and I want the application to not crash, can someone help me, in this case, please? If you find a solution post it here including your code and my code too please and thank you?!

 int num; Console.Write("Please type your number here:"); num = Int32.Parse(Console.ReadLine()); if (num < 0) Console.WriteLine("This is a negative number!"); if (num > 0) Console.WriteLine("This number is a positive number"); 
9
  • 1
    That number is too large to fit in an int, use long instead. Commented Feb 4, 2020 at 14:57
  • @DavidG I just typed that number instinctlly it was just an example I'm saying with a a number with more than 10 digits Commented Feb 4, 2020 at 14:59
  • 1
    Does this answer your question? Value was either too large or too small for an Int32 in Textbox Commented Feb 4, 2020 at 14:59
  • 1
    An int can only be up to 2147483647 whereas long will get you to 9223372036854775807. If you want bigger than that then look into BigInteger. Commented Feb 4, 2020 at 15:00
  • 1
    .NET has a BigInteger type that supports unlimited precision (well, as long as memory lasts anyway). System.Numerics.BigInteger.Parse("564984894897987878") works, as does System.Numerics.BigInteger.Parse("123456789012345678901234567890123456789012345678901234567890"). Of course, efficiency in calculating is much lower than with int, so it really depends what you want this number for -- if all you need is a string and you want to verify it contains only digits, use something else (like "1234".All(Char.IsDigit)). If all you want to test is the sign, the first character suffices! Commented Feb 4, 2020 at 15:01

2 Answers 2

3

If you want to represent an arbitrarily large integer, you should use BigInteger.

If you use int (aka System.Int32) you'll be limited to a range of -2147483648 to 2147483647 inclusive.

If you use long (aka System.Int64) you'll be limited to a range of -9223372036854775808 to 9223372036854775807 inclusive.

Now it could be that long is fine for you here - but a user would still be able to crash your app, or at least make it not work "as expected", pretty easily. With BigInteger you should be fine for any integer value that your computer has enough memory to store.

Your code can be converted almost trivially to use BigInteger - just use BigInteger.Parse instead of Int32.Parse, and then compare with BigInteger.Zero. To improve the code further, beyond just handling large integers, you could use BigInteger.TryParse to handle invalid inputs by gracefully reporting those to the user instead of crashing with a FormatException.

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

Comments

0

You can use TryParse:

 Console.Write("Please type your number here:"); if(!Int32.TryParse(Console.ReadLine(), out num)) Console.WriteLine("Invalid number"); if (num < 0) Console.WriteLine("This is a negative number!"); if (num > 0) Console.WriteLine("This number is a positive number"); 

3 Comments

That will stop it from crashing, but I don't think it's what the OP actually wants to do. I believe they want to be able to handle large integers, not just "not crash".
@JonSkeet you may be correct, in that case your solution is the correct one. But in any case parsing a string to any other value I would recomend the TryParse
Yes, using TryParse is a generally good idea, and I've added it to my answer. I do feel this question is primarily about handling larger integers rather than handling invalid input though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.