Home > Software design >  For some reason my code ends after hitting a do while loop(Java)
For some reason my code ends after hitting a do while loop(Java)

Time:09-10

if (choice == 2) {
    System.out.println("Current:");

    for (int i = 0; i < n; i  ) {
        System.out.println(values[i]   " ");
    }

    System.out.println("How many swaps?");
    int swaps = scan.nextInt();

    for (int i = 0; i<swaps; i  ) {
        int arr1 = (int) Math.random() * (n-1);
        int arr2 = (int) Math.random() * (n-1);

        System.out.println("random1");

        do {
            arr2 = (int) Math.random() * (n-1);
        } while (arr1 == arr2);

        System.out.println("random");

        int value1 = values[arr1];
        values[arr1] = values[arr2];
        values[arr2] = value1;

        System.out.println("swapped");

        for (i = 0; i<n; i  ) { 
            System.out.println(values[i]   " ");
        }
    }
}

System.out.println("swapped");

for (int i = 0; i < n; i  ) {
    System.out.println(values[i]   " ");
}

}

For some reason the code works up until the do while loop, where it stops, I put in printout commands to test this and the code always ends after stating random1. This isn't all of my code but just the problem area, so if you could give advice on how t0 fix this, maybe by using something other than a while loop or using some sort of break that would be great. Any advice is appreciated.

Edit: n is specified earlier in code not shown by the user to be the number of elements in an array(values), I am trying to pull two random numbers from that array by pulling two different ints(arr1 and arr2) from the values array(randomly generated ealier), then specifying those values and swapping them later in the code.

CodePudding user response:

Your code does not stop, you just created an endless loop.

Math.random() returns a number smaller than 1 (e.g.: 0.53432175...).
Because your int cast operator is written directly before your Method call of Math.random() you are casting the returned decimal value into an int.
However, int doesn't have any decimals, it just cuts them off.
So your remaining calculation looks like int arr1 = 0 * (n-1) which is 0.
That's why both of your variables int arr1 and int arr2 always have the same value. And in your do while loop you're then checking if 0 == 0 which is always true.
A fix for this is, if you first calculate and cast afterwards, you can do this as following:

int arr1 = (int) (Math.random() * (n - 1));

simply surround the calculation with a bracket. (Do the same for the int arr2 calculation.)

CodePudding user response:

@Anker's answer is spot on - but to give you an alternative you can try using Random.nextInt(bound) where bound is your n.

// Create one random instance.
java.util.Random r = new java.util.Random();

// and where ever you need a bounded random use the below 
// statement which produces a number [0, n)
//    which indicates from 0 (inclusive) to n (exclusive).
int myBoundedRandomInt = r.nextInt(n);

So your code would look generally like:

java.util.Random r = new java.util.Random();
for (int i = 0; i<swaps; i  ) {
    int arr1 = r.nextInt(n); // returns [0, n]
    int arr2;  // no need to generate since you do it in loop

    System.out.println("random1");

    do {
        arr2 = r.nextInt(n);
    } while (arr1 == arr2);

    //...
}
  •  Tags:  
  • java
  • Related