C# Help. Problem with convert | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C# Help. Problem with convert

Doesnt works https://code.sololearn.com/cv8qmsCxx6wd/?ref=app

2nd Mar 2018, 7:13 PM
DolphieDude
DolphieDude - avatar
8 Answers
+ 3
Sorry about the delay. I'm at work. I wrote up some code for you that may help you out. It'll take whatever type of string you throw at it, remove any spaces or letters, and then provide you with the int version of the input. If the input didn't contain any numbers, it'll let you know. You can easily integrate this into a loop system or isolate this as its own function. If you want me to show you how to do that, just let me know and I'll type up your code in a way that's utilizing the code I wrote. Let me know if you want me to explain anything! Best of luck to you. https://code.sololearn.com/c9IhBq5PG0Gy/#cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace SoloLearn { class Program { static void Main(string[] args) { int numberInput; string myInput = Console.ReadLine(); bool isNumber = int.TryParse(myInput, out numberInput); if(isNumber) { Console.WriteLine("Received number input. Int = " + numberInput); } else { Console.WriteLine("Converting string to int."); string convertedInput = Regex.Replace(myInput, @"[A-Za-z\s]+", ""); if(convertedInput == "") { Console.WriteLine("Input doesn't contain numbers."); return; } else { numberInput = int.Parse(convertedInput); Console.WriteLine("New Input = " + numberInput); } } } } }
2nd Mar 2018, 8:27 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
What exactly are you trying to do in that code? It seems like a really roundabout way to do: int Multiply = int.Parse(Console.ReadLine()); Multiply *= 2; Exactly your overall objective and specifications, and I'll be more than happy to help out if I can.
2nd Mar 2018, 7:47 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
You're more than welcome! Take care. If you don't understand after looking on Google, just ask and I'll explain.
2nd Mar 2018, 8:43 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Yes, parsing is like converting, but works a lot better when you're dealing with strings in C#. C# isn't as friendly as some other languages when you're trying to convert a string to a number. int numberInput; string myInput = Console.ReadLine(); bool isNumber = int.TryParse(myInput, out numberInput); ^As you thought, TryParse is going to try to parse it first and it'll store if it was successful or not in the boolean isNumber (1/true = success, 0/false = failure). We use TryParse here to help catch the exception and deal with it properly. The first argument of TryParse is the variable you want to try and parse, in this case it's our myInput variable that is storing the user's input. Once it parses the input, it'll store the output into the second argument (variable numberInput) which is already set up as an int variable.
2nd Mar 2018, 8:58 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@Jakob, if user print something what doesnt contain number program will be requested input again. P.s. Sorry for my english
2nd Mar 2018, 7:47 PM
DolphieDude
DolphieDude - avatar
+ 1
Thanks, good luck. It's time to google unknown methods👍
2nd Mar 2018, 8:31 PM
DolphieDude
DolphieDude - avatar
+ 1
@Jakob, Parse is like converting? TryParse returns result of attempt​?
2nd Mar 2018, 8:51 PM
DolphieDude
DolphieDude - avatar
+ 1
Thank you
2nd Mar 2018, 9:02 PM
DolphieDude
DolphieDude - avatar