thought i had it, then i sneezed and lost it (randomly generated numbers)

• July 17th, 2011, 02:03 PM
fakeClassy
thought i had it, then i sneezed and lost it (randomly generated numbers)
Hi - I am trying to learn java and have been given a code to randomly generate a number "n". I have been asked to do some math operations with nn (to be one copy of the randomly generated number right next to another of it.) so if n was 117 then nn would be 117117 and I am getting frustrated because it seems like it should be so easy to just put the two together without adding them.

I would appreciate the help
thnks
• July 17th, 2011, 02:26 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Quote:

to just put the two together without adding them.
The numbers you are talking about are in two different formats.
One is as a numerical value held in a variable like an int: 117.
The other format is as a String: "117"
The + operator works differently with numbers than it does with Strings.
With numbers it does addition: 11 + 22 => 33
With Strings it concatenates the two Strings: "A" + "B" => "AB"
• July 17th, 2011, 03:06 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
can i convert the int to a string, and if i do can i still use it for the math op's? I keep trying and getting all sorts of errors - i can get the two n's to print next to each other but i can't divide by nn as its own number - it always only divides by the second n. ugh
• July 17th, 2011, 03:12 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Quote:

can i convert the int to a string, and if i do can i still use it for the math op's
No. math ops require numbers

You'll have to post your code for any help on the rest. I have no idea what you are trying to do.
• July 17th, 2011, 03:16 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
public static void main(String[] args) {

int n = (int)Math.floor(Math.random()*1000+1);
int random = n;
int x = "random"+"random";
System.out.println("n =" +n);
System.out.println("NN =" +random+random);
System.out.println("NN divide by seven =" +x/7);
System.out.println("NN divide by eleven =" +x/11);
System.out.println("NN divide by thirteen =" +x/13);
• July 17th, 2011, 03:26 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Ok, I see the code. What next?
For example: show its output, describe what is wrong with it and show what you want it to be.
• July 17th, 2011, 03:34 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
If you could either hlp me with my errors or explain how to make it work, I would really appreciate it.
• July 17th, 2011, 03:36 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Quote:

hlp me with my errors
I will. Please post them with an explanation as I have requested.

Quote:

how to make it work,
Looking at the comments in the code I have no idea what the code is supposed to do. How can I suggest how to make the code work if there is nothting saying what it is supposed to do.
• July 17th, 2011, 03:44 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
I was given this problem "Write a program to randomly generate a 3-digit number N (it is ok if the number has
fewer than 3 digits). Create a number NN which is N placed next to it. Example, if
the number N is 263 then NN is 263263. If N is 7, NN is 007007. If N is 032, NN is
032032.
Next, divide NN by 7, and then by 11 and then 13.
Write the original number and the final result after the divisions.
Formula to randomly generate a 3-digit number is
(int)Math.floor(Math.random()*1000+1)
" and the code I posted was one of the latest attempts.
• July 17th, 2011, 03:46 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Ok, I'll ask you line by line to explain what you want your code to do.
What is this line supposed to do?
int x = "random"+"random";

Here are two choices that will allow the above to compile:
String x = "random"+"random";
or
int x = random+random;
• July 17th, 2011, 04:03 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
I put in the
int x = random+random;
and the program worked but, the divide by 7... part of the code divided the randomly generated number plus itself as opposed to a new number created by two of the randomly generated numbers next to each other.
if the randomly generated number "n" was 700 I am trying to divide by "nn" which would be 700700/7 = 100100
The way the code runs now for that I am getting 200 ( which would be 700+700 = 1400/7 )
• July 17th, 2011, 04:08 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Look at the resultant value that you want given 700.
How would you get that result if you did it in a calculator?
Shift the 700 left by 3 positions and add 700.
To shift a decimal number left you multiply by a multiple of 10. 3 positions would be by 1000.

Or you could convert the numbers to Strings, concatenate them and then convert the String back to a number. The String class and the Integer class have methods to do this.
• July 17th, 2011, 04:14 PM
fakeClassy
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
But the 700 was figurative not literal, it was an example of an output from the randomly generated number line of code.
I think this whole problem is how to randomly generate a number and then use two copies of it as a new number.
My brain hurts again but I do thank you for the help.
• July 17th, 2011, 04:16 PM
Norm
Re: thought i had it, then i sneezed and lost it (randomly generated numbers)
Quote:

But the 700 was figurative not literal,
How does that change my answer? If the value was 5 or 27 the same logic works. 5 would shift by 1 position, 27 by two.