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

Thread: Get out of loop to finish program?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get out of loop to finish program?

    This program is supposed to make 10 unique #s in an array, it outputs correctly, but it is stuck in the while loop. hHow can I get out?


    import java.util.Random;

    public class Extra {

    public static void main(String[] args) {

    // Variables
    int rand;
    int [] unique = new int [11];
    boolean [] check = new boolean [11];
    Random generator = new Random();

    // Welcome
    System.out.print("Here are your ten unique array values: ");

    // Set Arrays
    for(int counti = 0; counti < unique.length; counti++) {
    check[counti] = false;
    }

    // Set Array
    for(int count = 0; count < unique.length; count++) {
    if(check[unique[count]] == false){
    check[unique[count]] = true;
    System.out.print(unique[count] + " ");
    }
    else{
    while(check[unique[count]] == true) {
    unique[count] = generator.nextInt(10) + 1;
    if(check[unique[count]] == false){
    check[unique[count]] = true;
    System.out.print(unique[count] + " ");
    }
    if(count > 2){
    break;
    }
    }
    }
    }
    }
    /**
    Here are your ten unique array values: 437521689
    */


  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Get out of loop to finish program?

    while (check[unique[count]] == true) {
                        System.out.println("check[unique[count]] " +check[unique[count]] );
                        System.out.println("in while " + number);
                        unique[count] = generator.nextInt(10) + 1;
                        if (check[unique[count]] == false) {
                            check[unique[count]] = true;
                            System.out.print(unique[count] + " ");
                        }
                        if (count > 2) {
                            break;
                        }
                        number++;
                    }

    when do you set your check to false?

    add some print out lines like above...

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get out of loop to finish program?

    this is what the output turns to be:
    Here are your ten unique array values: 103127check[unique[count]] true
    in while 0
    9 check[unique[count]] true
    in while 0
    check[unique[count]] true
    in while 0
    8 check[unique[count]] true
    in while 0
    check[unique[count]] true
    in while 0
    4
    Process completed.




    and my check is set in the for loop :

    for(int counti = 0; counti < unique.length; counti++) {
    check[counti] = false;
    }

  4. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Get out of loop to finish program?

    check[unique[count]] true
    in while 0
    it is here that your problem is. you never set your check to false in your while loop to drop out.

    you can use a different while loop or not use it at all.

    put your display loop after your for loop set Array and you may see different results.


    after reworking your code (re-arranged)
    here is my results

    Here are your ten unique array values: 0 0 4 2 7 1 1 6 3 4 6 6
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by william; June 17th, 2011 at 08:36 AM. Reason: added code results

Similar Threads

  1. Can anyone help me finish creating a GUI for my program
    By danbendlin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2012, 07:34 PM
  2. Java Program Loop Possibly needed
    By mikec2500 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 25th, 2011, 12:35 AM
  3. Need a program using only for loop its urgent
    By pokuri in forum Object Oriented Programming
    Replies: 2
    Last Post: January 14th, 2011, 04:13 AM
  4. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 02:23 PM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM