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 7 of 7

Thread: Do While Loop

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Do While Loop

    First of all, hey, Im a bit new to this board, and really needed help so thought it would be a good place to get some help.

    Okay this is what i'm trying to do,

    I want to create 50 random numbers assigned to different values, and tried using a do while loop for this.
    E.g.

    randomNumber1 = 34;
    randomNumber2 = 43;

    but without having to declare 50 numbers by hand, and rather have a loop to create it for me.

    Here is the small bit of code for it that I wrote up:

    int generateNum = 0;
    int currrentValue = 2;
     
    do {
          int randomNumber = (int)Math.round((Math.random() * 365) + 1); 
          }
     
    while (generateNum < 49) {
     
           int randomNumber.currentValue = (int)Math.round((Math.random() * 365) + 1);
     
                        currentValue++;
                        generagteNum++;
           }
    However upon compiling it gives the error message " ; expected " on the "while (generateNum < 39) {" line.



    Anyone that can help me out of it?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Do While Loop

    Thats a very wierd piece of code if i do say so myself. Have a read up on Arrays, they will show you the light

    Chris

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Damn, didn't think about using an array. I think I should be able to do that quite easily. Thanks for the tip!

    And btw, any chance you can elaborate on the 'weird' piece of coding? I'm more or less a complete newbie so any help to improve my coding is greatly appreciated

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Do While Loop

    What is randomNumber.currentValue? never seen anything like it

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Do While Loop

    ... there should be a few compile errors in that code. do-while statements require a semi-colon after the while loop. Also, you're declaring two local variables with the same name (randomNumber). The int data type also doesn't have any sub-fields/methods, that's why it's a primitive data type in Java. Also, your do-while loop is generating a random number an infinite number of times without doing anything else. That will cause your program to hang, and also you really should do something each time a random number is generated.

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Do While Loop

    What you want to do is something like this.

    final int[] integers = new int[50]; // create the array to hold 50 primitive integers
    final Random random = new Random(); // create the random generator, can be found in java.util package
     
    // Loop 50 times and create a random number each time and assign it to the correct index in the array
    for(int index = 0; index < 50; ++index) {
        integers[index] = random.nextInt(); // generate number and store in the array
    }

    // Json

  7. The Following User Says Thank You to Json For This Useful Post:

    connex (December 7th, 2009)

  8. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Quote Originally Posted by Freaky Chris View Post
    What is randomNumber.currentValue? never seen anything like it
    I was trying to increment the currentValue by one each time, e.g. 1, 2, 3, then append it to randomNumber so it generates random variables like:

    randomNumber1
    randomNumber2
    etc...

    I think that functionality is with PHP which is why I got confused to why it didn't work.

    And thanks Json, I already worked out a solution using arrays, which is similar to the solution you gave me. Thanks for the help though!

    If there is a way to set this as solved, now would be the time to do it

Similar Threads

  1. loop and a half
    By Brain_Child in forum Loops & Control Statements
    Replies: 2
    Last Post: January 22nd, 2010, 03:52 AM
  2. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  3. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  4. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM

Tags for this Thread