• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert a number to a string?  RSS feed

 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a class problem to write some simple code but I am stumped. I have to randomly generate a 3-digit number N, then double it without adding, to make NN

(if N is 263 then NN is 263263)

then I have to do some other things with NN

My idea was to convert N to a string, then N+N to get the double number without adding, then convert the final number back to an integer

That is where I am stuck.

Any Thoughts??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it could be to multiply the first number by 1000 and add the result to the first number.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No adding allowed. Look at the Integer class. It has methods to parse (hint) a String into an int.
 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy c$%p that was easy!!! thanks

I was thinking wayyy to hard on this one.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote:No adding allowed.

I was caught cheating !
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*cough*

*cough*
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Another way to do it could be to multiply the first number by 1000 and add the result to the first number.


Isn't that the same as multiplying by 1001. No adding
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Christophe Verré wrote:Another way to do it could be to multiply the first number by 1000 and add the result to the first number.


Isn't that the same as multiplying by 1001. No adding


I'm not that smart you know
 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, the instructor did say "create a number NN" but he didn't say how! I turned it in like that so I guess we'll see if it's cool or not.

I knew from class notes that there was a way to parse a string back into an integer, but not the other way around.

Being a newbie (week 4) to programming, I get totally lost in the API. I'm sure it's all laid out logically but since it has no back arrow I have trouble establishing relationships between links....once I click on something I don't know where its taking me exactly or how to get back to where I was. And I don't really understand the tree structure.

But, I'm still hung up on methods and calls and stuff like that.

I want to understand this but it seems like at the beginning there's a lot of "just do this-we'll explain why later". I want to know now!


(Somewhere, I can hear Jack Nicholson saying "You can't HANDLE the truth!")
 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH I forgot to mention this class is totally online....no back and forth with the instructor and the class discussions are kind of general not to mention over my head anyway
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a way you can get familiar with the API. If you want to do something with a specific object, look up the class that is associated with it and look at all the methods it provides.
Over time you will remember a lot of the functions that the API has.

In your case, you wanted to do something with an integer value. The associated class is Integer. Look it up and check out all the methods it has. After you converted it to a String, you could have looked up String and looked at all the methods that it provided.

Good luck!
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Papa Miller,
Go ahead and write your code. If you run into problems, go ahead and post it here. And I am sure you will get a lot of feedback.

Gary
 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK that's where I was looking before, but I can't figure out how to get that from the API and into my program.

I tried a couple things like this:


with lots of error messages as you can probably tell.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the right concept but the wrong code.
Follow Stephan's instruction:

Here is my approach.
1. Create random numbers.
2. Convert an int to Integer.
3. Convert Integer to String using toString (2 && 3 can be combine, maybe 1 also).
4. output the string N + N.

Hope this helps,
Gary
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to use math.floor().
Multiplying the return value of Math.random() by 1000 and casting to an int will give you an int between 0 and 999.

Once you've done that you then have the choice of either multiplying the result by 1001 or using Integer.toString(int) to get a String and then concatenating it with itself.

Both these approaches have the problem that numbers between 0 and 99 will need some extra processing. You need to prefix the number with "0" or "00" before concatenating.

A method that nobody has suggested yet, that will handle 1 and 2 digit numbers with no extra processing is to use String.format().
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ideas are fine, just executed poorly. Firstly you need to use the toString code I posted for you above, here it is again just with your n put in and a string named nString:


Now you still have the problem of not really generating just 3 digit numbers, you can solve this with a while loop around a generator verifying the result is within your 3 digit range:


This is the easiest way to get 3digits every time, but it does however not allow for something which you may or may not care about. It does not give you results from 001 - 099 as 3 digit numbers. If you care about that you could do the same thing without the while loop and just do an if statement to add "0" or "00" to the number when the numbers are less than 100. Do that once it is a string of course, else the 0's will be dropped when you convert to String. Or use Joanne's suggestion of using String.format() to achieve the same thing.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!