This site requires JavaScript, please enable it in your browser!
Greenfoot back
slchoiaa
slchoiaa wrote ...

2016/10/31

How to change int to String??

slchoiaa slchoiaa

2016/10/31

#
I have a method that int getRandomNumber(int lowerBound, int upperBound) getRandomNumber(1, 9) means that i generate a integer number from 1 to 9 if I want to make String secret = 2 interger number of getRandomNumber(1,9) , how can i do for it???
Super_Hippo Super_Hippo

2016/10/31

#
I think this should work. I am not sure if the second "" is needed there.
1
String secret = "" + getRandomNumber(1,9) + "" + getRandomNumber(1,9);
danpost danpost

2016/10/31

#
Super_Hippo wrote...
I am not sure if the second "" is needed there.
No, it is not needed. The first set is to tell the compiler that you are creating/building a String object; and you are continuing to build the same object with the second random number.
Nosson1459 Nosson1459

2016/11/1

#
slchoiaa wrote...
I have a method that int getRandomNumber(int lowerBound, int upperBound) getRandomNumber(1, 9) means that i generate a integer number from 1 to 9 if I want to make String secret = 2 interger number of getRandomNumber(1,9) , how can i do for it???
y would u want 2 convert a string to an int
danpost danpost

2016/11/1

#
Nosson1459 wrote...
y would u want 2 convert a string to an int
That is not what is happening here. We are taking numeric values and adding their digital characters to a string. However, there are several good reasons why one might want to convert a numeric string into a number. This is often done when a numeric value is input as a string or read as a string from file data.
You need to login to post a reply.