look for consecutive numbers in a closed list

I think you are on the right track, your thought process with coming up with that pseudocode was extremely easy to follow, great job 💯🔥:

import java.util.Arrays;
import java.util.Scanner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class Main {
  public static void main(String[] args) {
    assertTrue(fourConsecutive(new int[] {1, 2, 3, 4, 6, 9}));
    assertTrue(fourConsecutive(new int[] {1, 2, 4, 5, 6, 7, 8}));
    assertTrue(fourConsecutive(new int[] {3, 2, 4, 5, 6, 7, 9, 1, 8}));
    assertFalse(fourConsecutive(new int[] {5, 2, 4, 1, 7}));
    assertFalse(fourConsecutive(new int[] {1, 2, 3, 5, 6}));

    try (Scanner scanner = new Scanner(System.in)) {
      int arrayLength =
          getIntegerInput(scanner, "How many numbers are you going to enter: ", 0, 10);
      int[] array = new int[arrayLength];
      for (int i = 0; i < arrayLength; i++) {
        array[i] = getIntegerInput(scanner, String.format("Enter number %d: ", i + 1), 1, 9);
      }
      System.out.println(
          fourConsecutive(array)
              ? "There is a sequence of four consecutive numbers in the numbers you entered: "
                  + Arrays.toString(array)
              : "There is a not a sequence of four consecutive numbers in the numbers you entered: "
                  + Arrays.toString(array));
    }
  }

  private static boolean fourConsecutive(int[] arr) {
    for (int i = 0; i < arr.length - 3; i++) {
      if (arr[i + 1] == arr[i] + 1 && arr[i + 2] == arr[i] + 2 && arr[i + 3] == arr[i] + 3) {
        return true;
      }
    }
    return false;
  }

  private static int getIntegerInput(Scanner scanner, String prompt, int minValue, int maxValue) {
    System.out.print(prompt);
    int validInteger = -1;
    while (scanner.hasNext()) {
      if (scanner.hasNextInt()) {
        validInteger = scanner.nextInt();
        if (validInteger >= minValue && validInteger <= maxValue) {
          break;
        } else {
          System.out.printf(
              "Error: Please enter an integer between %d and %d inclusive%n", minValue, maxValue);
          System.out.print(prompt);
        }
      } else {
        System.out.printf(
            "Error: Please enter an integer between %d and %d inclusive%n", minValue, maxValue);
        System.out.print(prompt);
        scanner.next();
      }
    }
    return validInteger;
  }
}

Example Usage:

How many numbers are you going to enter: 14
Error: Please enter an integer between 0 and 10 inclusive
How many numbers are you going to enter: 8
Enter number 1: 12
Error: Please enter an integer between 1 and 9 inclusive
Enter number 1: 3
Enter number 2: 8
Enter number 3: 4
Enter number 4: 5
Enter number 5: 6
Enter number 6: 7
Enter number 7: 1
Enter number 8: 9
There is a sequence of four consecutive numbers in the numbers you entered: [3, 8, 4, 5, 6, 7, 1, 9]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top