C
C#2w ago
Moon

why is convert.toint32 must be there? and not just console.readline (line 4)

Console.WriteLine("whats ur name"); string name = Console.ReadLine(); Console.WriteLine("age?"); int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo");
36 Replies
Jimmacle
Jimmacle2w ago
what does Console.ReadLine() return? any line
Moon
MoonOP2w ago
i just started so if i got right ur question, the first one is when u type ur name and the second is for age
Jimmacle
Jimmacle2w ago
right, but what kind of data does it give you?
Moon
MoonOP2w ago
to answer and fill information no?
Jimmacle
Jimmacle2w ago
i'm talking about data types in the language, those affect what you can do with the data Console.ReadLine() returns a string which is just text it doesn't know if it's a number or not, so if you want to use the data like a number you have to convert it to a different data type used to represent numbers in your specific example there's no real point because you just print it back out, but if you wanted to do math with the age or something you would need it
Moon
MoonOP2w ago
but why do u need to use convert?
Jimmacle
Jimmacle2w ago
if you want to use the data like a number you have to convert it to a different data type used to represent numbers
try writing the code without convert and see what happens as an experiment
Moon
MoonOP2w ago
it works
Jimmacle
Jimmacle2w ago
then you changed more than just removing convert
Moon
MoonOP2w ago
Console.WriteLine("whats ur name"); string name = Console.ReadLine(); Console.WriteLine("age?"); int age = Console.ReadLine();
Console.WriteLine("hello" + name); Console.WriteLine("ure" + age + " yo"); its the same
Jimmacle
Jimmacle2w ago
that code doesn't compile why did you say it works? do you see a red squiggly line on the int age = Console.ReadLine(); line?
Moon
MoonOP2w ago
yeah yeah it dosent work
Jimmacle
Jimmacle2w ago
because Console.ReadLine gives you a string back, not int if you want an int you have to convert the string to one
Moon
MoonOP2w ago
im sorry that it takes me a whil,e i dont speak engllish and my mind is blown up console read line always gives string?
Jimmacle
Jimmacle2w ago
always
Moon
MoonOP2w ago
so when i type back i cant write like hello world just helloworld
Jimmacle
Jimmacle2w ago
strings can have spaces in them ReadLine reads a line of text, which means everything you type until you press enter
Moon
MoonOP2w ago
theres one without space i forgot ohhhh so when u do invert it makes the readline able to use int
Jimmacle
Jimmacle2w ago
not really
Moon
MoonOP2w ago
bro what
MODiX
MODiX2w ago
Moon
console read line always gives string?
React with ❌ to remove this embed.
Jimmacle
Jimmacle2w ago
readline never uses int but you can take the string that readline gives you, and give it to convert.toint32 because that takes in a string and gives you back an int but it only works if the string actually has a number typed into it
MODiX
MODiX2w ago
Jimmacle
REPL Result: Failure
Convert.ToInt32("hello")
Convert.ToInt32("hello")
Exception: FormatException
- The input string 'hello' was not in a correct format.
- The input string 'hello' was not in a correct format.
Compile: 412.379ms | Execution: 18.820ms | React with ❌ to remove this embed.
Moon
MoonOP2w ago
bro its so hard so it dosent make consolereadline able to use int?
Jimmacle
Jimmacle2w ago
correct, you can't change how readline works but you can take what readline gives you and change it in your code to be something else that's what convert.toint32 does
Moon
MoonOP2w ago
wdym by something else
Jimmacle
Jimmacle2w ago
like an int
Moon
MoonOP2w ago
wdym something else, to change the readline?
Jimmacle
Jimmacle2w ago
again, you can't change what readline does but you can do things to what readline gives you
string text = Console.ReadLine();
int textAsNumber = Convert.ToInt32(text);
string text = Console.ReadLine();
int textAsNumber = Convert.ToInt32(text);
because different data types mean different things, strings are text and ints are numbers
MODiX
MODiX2w ago
Jimmacle
REPL Result: Success
"9" + "10"
"9" + "10"
Result: string
910
910
Compile: 147.528ms | Execution: 15.932ms | React with ❌ to remove this embed.
MODiX
MODiX2w ago
Jimmacle
REPL Result: Success
9 + 10
9 + 10
Result: int
19
19
Compile: 149.307ms | Execution: 14.743ms | React with ❌ to remove this embed.
Moon
MoonOP2w ago
u made the string here a number?
Timmy
Timmy2w ago
Convert is a class in this case which ahas functions, ToInt32 is a function, which takes a string as input. Both string and int are types. string text = Console.ReadLine(); string is a type text is the variable Console is the class and ReadLine is the function. The return type of the function specifies what it gives back. void just simply means nothing, but in this case, ReadLine returns a string. int textAsNumber = Convert.ToInt32(text); int is a type textAsNumber is the variable Convert is the class and ToInt32 is the function. The ToInt32 function takes a string parameter, in this case the texxt variable which is a string.
Jimmacle
Jimmacle2w ago
it's the same thing you're doing with age, just split into 2 lines
JP
JP2w ago
at the risk of beating a dead horse, the code there says, in english:
1. Read one line of text from the console and store it in the box labeled "text" 2. Take the value in the box labeled "text", convert it into an int, and then store that int value in the box called "textAsNumber"
Moon
MoonOP2w ago
i understood it, thanks yall very much

Did you find this page helpful?