Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

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

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

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

    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"

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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
    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.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

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

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

    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.

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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;
    Last edited by Norm; July 17th, 2011 at 03:50 PM.

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 )

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

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

    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.

Similar Threads

  1. Code working, but not the way I thought...
    By JLogan3o13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2010, 12:34 PM
  2. unable to view the result generated by stored procedure
    By najmudeenhaja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2010, 08:39 AM
  3. I'm lost
    By stoptheerrors in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2010, 08:47 AM
  4. [SOLVED] I thought that this would be right?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 03:26 PM
  5. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM

Tags for this Thread