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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
If we look at the documentation for the method, we see that Math.random() produces a double number in the range [0.0, 1.0). This means that 0.0 is included in the range but 1.0 is not included in the range.
So, Math.random() will give you [0.0, 1.0) Math.random() * 10 will give you [0.0, 10.0) (int)(Math.random() * 10) will give you [0, 10) (int)(Math.random() * 10) + 1 will give you [1, 11) i.e. an integer which can be any of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
double randomNumber = Math.random();
// Stores a random decimal between 0 and 1 in randomNumber
double number = String.valueOf(“2.5”);
// Transforms the String “2.5” into a double
In the above example shows that we can change or transform String value into a double by using that method but when i try the same it throws me an error stating…
ExerciseOne.java:13: error: incompatible types: String cannot be converted to double
double number = String.valueOf(“1.7”);
^
1 error
Very good catch. After trying to replicate why you are getting the error, I looked at the documentation. Turns out Codecademy made a mistake and got things mixed up.
If we want to transform the String “2.5” into a double, then we should use: double number = Double.valueOf("2.5");
If we want to transform the double 2.5 into a String, we should use: String number = String.valueOf(2.5);
So, Codecademy got things the other way around. I have submitted a bug report for the content. Hopefully, they will fix it. You may also want to submit a bug report via the “Get Unstuck” dropdown for the exercise.
Oh…really thanks for the reply and examples that you provided was spot on. Because of the example i just understand what the valueOf would do when we code but i have one more doubt in the same exercise
String myNewString = Integer.toString(1);
As per my understanding about the above method could change integer value into string right(Idk but i just look the code and understand that is how it work) but i want to know the real purpose of using the above method…and Thank You…
Another way of doing the same would be to use the valueOf method of the String class. String myNewString = String.valueOf(1);
As mentioned in a remark in the documentation,
The representation is exactly the one returned by the Integer.toString method of one argument.
So, you have a choice on how you want to do the conversion. (Edit: I don’t know which of the two approaches would be preferable/better).
One method is from the String class, the other is from the Integer class. So, it shouldn’t be surprising that some methods from different classes may offer us alternative ways of accomplishing the same thing.
I am also thinking the same. Since both do the same job(conversion). That why am ask is there anything special in it or we get a different output when we use. Now your reply saved me and i understand we have optional to do so. Thank for the conformation…
Thank you very much…