简体   繁体   中英

While loop not breaking when its condition isn't met

The program should print "The number 11 occurs more than once" Then set duplicateFound to true and exit the while loop because it is true. But it prints "The number 13 occurs more than once" also.

Why does this happen and how do I break the while loop so it does not keep going once the first duplicate (11) is found? This is an issue of efficiency as I only want to know if a duplicate exists. I don't care about how many.

public class DuplicateNumbers
{
    public static void main(String[] name)
    {
        int[] myArray = { 64, 17, 9,  55, 2, 11, 11, 1,  8, 23, 13, 13};
        boolean duplicateFound = false;
        while (duplicateFound == false) 
        {
            for(int i = 0; i < myArray.length; i++) 
            {           
                for(int j = i + 1; j < myArray.length; j++)
                {
                    if(myArray[j] == myArray[i])
                    {
                        System.out.println("The number " + myArray[i] + " occurs more than once.");
                        duplicateFound = true;
                    }
                }
            }
        }
        if(duplicateFound == false)
        {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

Here's a more clear way to write this. Notice it removes what you might see as the need for the while loop. It uses a return statement in the for loops instead of a break , which in my opinion is a cleaner way of handling results in loops.

public class DuplicateNumbers
{
  private static boolean hasDuplicates(int[] myArray) {
    for(int i = 0; i < myArray.length; i++) 
    {           
      for(int j = i + 1; j < myArray.length; j++)
      {
        if(myArray[j] == myArray[i])
        {
           System.out.println("The number " + myArray[i] + " occurs more than once.");
           return true;
        }
      }
    }
    return false;
  }

  public static void main(String[] name)
  {
     int[] myArray = { 64, 17, 9,  55, 2, 11, 11, 1,  8, 23, 13, 13};
     if (!hasDuplicates(myArray)) {
        System.out.println("There are no duplicates.");
    }
    System.out.println("Array length is " + myArray.length);
  }
}

use break to leave the loops. Cause you have nested loops, you need named loops and a named break:

as of the comments: You dont need the While loop. If no duplicate is found, you dont want to reiterate all the arrays again.

 outerFor: for(int i = 0; i < myArray.length; i++) 
 {           
    for(int j = i + 1; j < myArray.length; j++)
    {
        if(myArray[j] == myArray[i])
        {
           System.out.println("The number " + myArray[i] + " occurs more than once.");
           duplicateFound = true;
           break outerFor;
        }
    }
  }

Actually you dont need the break at all. It just saves you some calculation time, when you encounter a duplcate at the very beginning.

The problem is that your for loops do not test on the duplicateFound condition. If you want to exit each loop when a duplicate occurs you can do the following:

public class DuplicateNumbers {
    public static void main(final String[] name) {
        final int[] myArray = { 64, 17, 9, 55, 2, 11, 11, 1, 8, 23, 13, 13 };
        boolean duplicateFound = false;
        while (duplicateFound == false) {
            for (int i = 0; i < myArray.length && !duplicateFound; i++) {
                for (int j = i + 1; j < myArray.length && !duplicateFound; j++) {
                    if (myArray[j] == myArray[i]) {
                        System.out.println("The number " + myArray[i]
                                + " occurs more than once.");
                        duplicateFound = true;
                    }
                }
            }
        }
        if (duplicateFound == false) {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

Another option would be to use a label and break statement, although generally I'm not a huge fan of this sort of unstructured code.

For example:

public class DuplicateNumbers {
    public static void main(final String[] name) {
        final int[] myArray = { 64, 17, 9, 55, 2, 11, 11, 1, 8, 23, 13, 13 };
        boolean duplicateFound = false;
        whileLoop: while (duplicateFound == false) {
            for (int i = 0; i < myArray.length; i++) {
                for (int j = i + 1; j < myArray.length; j++) {
                    if (myArray[j] == myArray[i]) {
                        System.out.println("The number " + myArray[i]
                                + " occurs more than once.");
                        duplicateFound = true;
                        break whileLoop;
                    }
                }
            }
        }
        if (duplicateFound == false) {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

You should just break; once you have found the duplicate.

System.out.println("The number " + myArray[i] + " occurs more than once.");
duplicateFound = true;
break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM