1

The statement before the begining of while loop System.out.println("Value of i before loop = " + i); is not being printed and the value of i in the loop is not being printed starting from 1. Instead it starts printing from a random big int.

package main;

import java.util.Random;

public class Main {
    public static void main(String args[]){

        Random ran = new Random();

        int[] in = {2,5,9};
        int[] c_gen = new int[3];
        int i = 0;

        System.out.println("Value of i before loop = " + i);

        while(!(c_gen.equals(in))){
            c_gen[0] = ran.nextInt(10);
            c_gen[1] = ran.nextInt(10);
            c_gen[2] = ran.nextInt(10);
            i++;
            System.out.println(c_gen[0] + " " + c_gen[1] + " " + c_gen[2] + "    .................." + i);
        }

        System.out.print("in = ");
        for(int x : in)
            System.out.print(x + " ");

        System.out.print("\n" + "c_gen = ");
        for(int x : c_gen)
            System.out.print(x + " ");

        System.out.println("\n" + "i = " + i);
    }
}
Necreaux
  • 9,451
  • 7
  • 26
  • 43
XRay
  • 13
  • 4
  • 1
    I dont see any such behaviour in your [code](http://ideone.com/zDe7Ll) as you described infact you have an infinite loop it means while condition is always true so you need to search for how to compare array – singhakash May 21 '15 at 15:19

3 Answers3

3

You are directly comparing arrays resulting in an infinite loop. Those results are being printed but are going to be at the top of tons and tons of output. Fix your comparison.

Necreaux
  • 9,451
  • 7
  • 26
  • 43
0

I get:

Value of i before loop = 0
2 2 1    ..................1
2 2 4    ..................2
...

Suggest you rebuild the project and try again.

As originally posted your code will not terminate because int[].equals(int[]) will not do what you expect.

You could try this though.

private static boolean equals(int[] a, int[] b) {
    if (a == null && b == null) {
        // Both null
        return true;
    }
    if (a == null || b == null) {
        // One null
        return false;
    }
    if (a.length != b.length) {
        // Differ in length.
        return false;
    }
    for (int i = 0; i < a.length; i++) {
        if (a[i] != b[i]) {
            // Mismatch
            return false;
        }
    }
    // Same.
    return true;
}


public void test() {
    Random ran = new Random();

    int[] in = {2, 5, 9};
    int[] c_gen = new int[3];
    int i = 0;

    System.out.println("Value of i before loop = " + i);

    while (!equals(c_gen, in)) {
        c_gen[0] = ran.nextInt(10);
        c_gen[1] = ran.nextInt(10);
        c_gen[2] = ran.nextInt(10);
        i++;
        System.out.println(c_gen[0] + " " + c_gen[1] + " " + c_gen[2] + "    .................." + i);
    }

    System.out.print("in = ");
    for (int x : in) {
        System.out.print(x + " ");
    }

    System.out.print("\n" + "c_gen = ");
    for (int x : c_gen) {
        System.out.print(x + " ");
    }

    System.out.println("\n" + "i = " + i);
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Sotirios' intuition is correct - your bug is in the line while(!(c_gen.equals(in))). You can't compare arrays for equality using the .equals(...) method because "arrays inherit their equals-method from Object, [thus] an identity comparison will be performed for the inner arrays, which will fail, since a and b do not refer to the same arrays." (source). Thus because c_gen and in will always refer to different arrays (even if their contents are the same), your loop will go forever.

Try Arrays.equals(..) instead:

public static void main(String[] args) {
    Random ran = new Random();

    int[] in = {2,5,9};
    int[] c_gen = new int[3];
    int i = 0;

    System.out.println("Value of i before loop = " + i);

    while(!Arrays.equals(in, c_gen)){

        c_gen[0] = ran.nextInt(10);
        c_gen[1] = ran.nextInt(10);
        c_gen[2] = ran.nextInt(10);
        i++;
        System.out.println(c_gen[0] + " " + c_gen[1] + " " + c_gen[2] + "    .................." + i);
    }

    System.out.print("in = ");
    for(int x : in)
        System.out.print(x + " ");

    System.out.print("\n" + "c_gen = ");
    for(int x : c_gen)
        System.out.print(x + " ");

    System.out.println("\n" + "i = " + i);

}

This works (terminates in finite time) for me, with sample output:

Value of i before loop = 0
1 9 9    ..................1
5 4 1    ..................2
1 1 6    ..................3
1 3 6    ..................4
.... //Omitted because of space
6 5 8    ..................1028
2 5 9    ..................1029
in = 2 5 9 
c_gen = 2 5 9 
i = 1029
Community
  • 1
  • 1
Mshnik
  • 7,032
  • 1
  • 25
  • 38