❔ ✅ Inputing numbers
I want to write a short code that gets random input. It reads two numbers between random inputs.
im somewhere around here.. but i havent figured the minus numbers also... the input can be something like:
22 Replies
Use
int.TryParse()
if you want to support negative numberscould you explain more this is my first code in c#... i come from python xd
also new to C languages
$tryparse
The TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
true
or false
to inform you if it succeeded or not, so you can use it directly in a condition,
- since C# 7 you can declare a variable that will be used as an out
argument inline in an argument list,
- it forces you to check if the out
argument contains valid data afterwards,
Avoid: Convert.ToInt32 — it's a bad choice for parsing an int
. It exists only for backwards compatibility reasons and should be considered last resort. (Note: Convert does contain useful conversion methods: To/FromBase64String
, To/FromHexString
, ToString(X value, int toBase)
, ToX(string? value, int fromBase)
)
Avoid: int.Parse — you have to use a try
/catch
statement to handle invalid input, which is a less clean solution.
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
@Angius i though i should have another variable that checks if its minus sign and if the following chars are number then its negative
would int.TryParse() work even on a str like: ag-123gago
No
Angius#1586
REPL Result: Success
Console Output
Compile: 610.725ms | Execution: 87.333ms | React with ❌ to remove this embed.
well i want my code to spit out -123 so should i still use int.TryParse
Ah, you want to pull out numbers from random gibberish?
As in, you want
usyadgfuyawi4rdsfjhg
to give you 4
?yes
i want to pull out 2 numbers from gibberish and sum them up
but they can be like negative
so i have to check if there is a minus sign before the number began
You could just try regex
Or, sure, build some sort of a parser
something like that
dont know what regex is
i got this but when i input 1 and 2 in terminal it prints out 0
Remember that it's not Python
What you have here is
not
okey thanks it works now
can it be written more elegantly tho
like the same thing but so much better and cleaner?like programs in python
@Angius also if i have only integers also negative on input and i want to put it inside an array should i use the code that you pasted the regex one?
You can use regex, sure
dont know why you use queue
tho
To make parsing easier
Also, the code there is not regex
c# harder then cpp
!close
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.