FAQ: Data Types and Variables - Converting Data Types

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Converting Data Types” exercise from the lesson "Data Types and Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Converting Data Types

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

Please help me. I get stuck in this particular problem and got this hint
“int variableName = (int)value;”
and I cant quite understand the value. What should I put there or leave there.

Im not sure what I did, I got stuck too! I think I used this:
int faveNumber = (int)Console.ReadLine();
not sure though, sorry.

3 Likes

very helpful thank you I didn’t know what to do lmao

Ty. It actually worked wow the hint made it worst fk me. Idk how long i was stuck there 4.

OMG THANK YOU! THE solution is wrong

So the user’s expected to first type

string variableName = Console.ReadLine();

Even though the introduction told the user that this won’t work and they should write something like

int variableName = (int)Console.ReadLine();

Which is what I did, and got stuck for a while because the lesson insisted that I make the mistake first. The hint told me that I should write

string variableName = Console.ReadLine();

which I also got stuck on for a while because I was confused. After I eventually agreed to do it this way, and got to the third part, I tried to do it by again defining a separate variable as the input and then feeding that to the converter, something like this:

string variableName = Console.ReadLine();
int faveNumber = Convert.ToInt32(variableName);

but the lesson didn’t accept it because the user is now expected to put Console.ReadLine(); directly into the converter, contrary to what the first hint told.

So this lesson is being pretty unfair.

8 Likes

somebody plz give me the answer for that i cant do it

Hello, @shaizx12! Welcome to the forum. The instructions for this exercise are not very clear. Which step are you on? If you can post your code, and the step you’re on, I’ll try to help you walk through it.

It gives an error even with the suggested solution:

Console.Write("Enter your favorite number!: ");

  // Turn that answer into an int
  
  // Attempt 1: use implicit conversion
  // int faveNumber = Console.ReadLine();
  
  // Attempt 2: use explicit conversion
  // int faveNumber = (int)Console.ReadLine();
  
  // Attempt 3: use Convert method
  int faveNumber = Convert.ToInt32(Console.ReadLine());

If you input anything other than an integer there is an error which not happen?

The first 2 attempts are supposed to generate errors in the console. The exercise is walking us through a couple of methods that don’t work before using the 3rd method which does work. However, there is a bug in the lesson that generates SCT errors even when you do exactly what is expected. The SCT errors are the messages that appear at the bottom of the code editor. They also fill in X’s in the check boxes before you have even attempted steps 2 or 3. I can assure you that the Codecademy engineering team is working on this bug.
For now, just make sure you understand the content, click “Get Solution” if you have to, and move on. If you have questions regarding the content feel free to ask, and I’ll do my best to explain. Happy coding!

2 Likes

what does the (int) operator actually do? because i have never seen such operator before.

Hello, @rubywhiz86690.

Welcome to the forum.

In the lesson, the (int) operator is a cast operator. It is required to explicitly cast one type to another type. As the lesson demonstrates, not every type can be cast to every other type. See this link to the official Microsoft Docs for more information, and examples.

Hey I finished the task but I got this error message pls click the link below.

Theres no spacing between the last line of your code!

favenumber=Convert.ToInt32(Console.ReadLine());

I’m not quite understanding how to prompt the user for input? Kindly review the screenshot below and let me know how to prompt for user input. (In the below example system defaults to 0 and prints 0, which I want to be able to customize and request from the user).

I can tell the writers know this lesson is confusing and bugged. I’d just like to say that while I think while actual idea behind this lesson is good its really frustrating to work through.

Intending for the user to fail is, to sugar-coat it, not necessarily the best methodology for teaching.

Spent ten minutes trying variations on the first bit of code:

To start, below the Console.Write() statement, create an int variable named faveNumber and set it equal to Console.ReadLine() .

I kept trying to figure out where I was going wrong and trying to bruteforce with new things, since I was trying to avoid the hints and not scroll down on the left bar.

(Also, the error message goes off past the right edge of the screen, even when fullscreened, and there’s no scroll to the right.)

I originally thought I was wrong in using the “Guess and Check” method but yes this is not an effective way of teaching. In one of the prior lessons it taught the student to clean up incorrect code - this one is setting you up for failure and then testing to see how well you can hit your head against the book until it works.

I have no idea about anything with C# and there was no real way for me to understand this. (Luckily I did the HTML course and it helped me know how to better place things, and GUESS correctly (after 200 or so failures.))

If the lesson is wrong, fix the lesson - don’t just acknowledge it, as it is in many replies to people complaining about it here.

So yeah.

faveNumber = Console.ReadLine();

into

int faveNumber = (int)Console.ReadLine();

Then finally the hint says:

int number = Convert.ToInt32(“string value”);

when the solution is instead:

faveNumber = Convert.ToInt32(Console.ReadLine());

Hope this helps you guys get past this and fully understand what Codecademy was trying to convey.

1 Like

I normally wouldn’t just put my answer here. But this exercise is so confusing and frustrating I’m gonna make an exception. Unfortunately it’s difficult to just put an answer since this exercise has 3 parts, but here’s what eventually worked for me:

static void Main(string[] args)
{
  // Ask user for fave number
  Console.Write("Enter your favorite number!: ");
  Console.ReadLine();

  // Turn that answer into an int
  //int faveNumber = (int)Console.ReadLine();
  int faveNumber =  Convert.ToInt32(Console.ReadLine());


}

Very disappointed they put this exercise in so early in the course. Very confusing and frustrating.