Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have this C# code here, and what Im 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. 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?
C#
int num;
            Console.Write("Please write your number here:");
            num = Int32.Parse(Console.ReadLine());
            


            if (num < 0)
                Console.WriteLine("This number is a positive number!");

            if (num > 0)
                Console.WriteLine("This number is a positive number!");


What I have tried:

I have tried to change the .Parse command but it didn't work I even tried to change the datatype but it didn't work please someone help me!?
Posted
Updated 4-Feb-20 4:51am
v2
Comments
F-ES Sitecore 4-Feb-20 6:30am    
Do you want it not to crash? Or do you want it to properly process the number? Those are two very different things.

Don't use Int32.Parse, use Int32.TryParse Method (System) | Microsoft Docs[^] instead. That will reject any number that is invalid or out of range, but without raising an exception.
 
Share this answer
 
Comments
Member 14734681 4-Feb-20 8:25am    
I used TryParse too but didn't work
Richard MacCutchan 4-Feb-20 8:59am    
Well unless you show us your code, and explain what you mean by "didn't work", then we cannot suggest anything.
Richard MacCutchan 4-Feb-20 9:02am    
Also, I have just tried your code, and apart from the fact that it says negative numbers are positive, it works fine. I think you are not showing us everything.
Member 14734681 4-Feb-20 9:27am    
Do u have discord dude?
Richard MacCutchan 4-Feb-20 9:37am    
What?
Number types have their limits. Use the long type instead, or the unlimited BigInteger.

Integral numeric types - C# reference | Microsoft Docs[^]

BigInteger Struct (System.Numerics) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 14734681 4-Feb-20 8:24am    
I tried both of them but didn't work
jimmson 4-Feb-20 8:38am    
Can you elaborate?
Member 14734681 4-Feb-20 9:26am    
I don't know how to elaborate more it just didn't work, could help me please copy paste my code and then try to debug it and find a solution that would help me a lot man <3
jimmson 4-Feb-20 9:30am    
Sure I can do that! For paying clients. This is a free help. Take it or leave it.
564 984 894 897 987 878 is too big a number to fit into a 32-bits signed integer.
Integral numeric types - C# reference | Microsoft Docs[^]
Use an Int64 or UInt64 instead.
C#
long num;
if (Int64.TryParse(Console.ReadLine(), out num))
{
   // Here the parsing succeeded
   if (num < 0)
      Console.WriteLine("This number is a negative number!");

   if (num > 0)
      Console.WriteLine("This number is a positive number!");
}
else
{
   Console.WriteLine("Input string is not a valid 64-bits integer.");
}
Edit:
Using TryParse allows you to handle parsing and provide a valid behaviour, whether or not intput string is valid. Never ever trust user inputs.
 
Share this answer
 
v2
Comments
Member 14734681 4-Feb-20 8:46am    
Could apply it to my code and send it as a solution ?
Member 14734681 4-Feb-20 8:47am    
Could you*
phil.o 4-Feb-20 10:51am    
I updated my solution a bit.
The Exception Message is telling you what the problem is; the number is too large for the assigned type, Int32.
Reading the documentation for the int.Parse() method will confirm this
MS Documentation

s represents a number less than MinValue or greater than MaxValue.
Now if you would have a try...catch block you could have caught that error, and properly reported it back
C#
int num;
Console.Write("Please write your number here:");
try {
	num = Int32.Parse(Console.ReadLine());

	if (num < 0) { Console.WriteLine("This number is a positive number!"); }
	if (num > 0) { Console.WriteLine("This number is a positive number!"); }
}
catch (OverflowException) { Console.WriteLine("Error: this number is way too big for an Int32!":); }
Also.. you may want to check your logic
if (num < 0) ...Console.WriteLine("This number is a positive number!");

Reference:
Int32.Parse Method (System) | Microsoft Docs[^]
OverflowException Class (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900