Search code examples
javaarraysmethodslinear-search

Search and count for single random number in array of 20 integers


I have a program that produces an array of 20 integers. Then produces one single random number in the same span. What I am attempting to do is search the array of 20 integers and count how many times this single random number occurs and print the result or if the integer is not present to display "not present". I feel like I am so close but I am getting an incorrect count for the random integer. Thank you in advance. Perimeteres for the search method: • The method datatype is void because no data will be returned.

• It will have three parameters – the integer array, the size of the array, and the value it will be searching for in the array.

• It will search through the array and count how many times the value was found.

• It will then either print out how many times the value was found or that the value was not found.

My code and sorry if it is not indented properly, still figuring this out:

import java.util.Scanner;
import java.util.Random;
class Main{
 public static final Random RND_GEN = new Random();
   public int[] createNum(int[] randomNumbers) {
    for (int i = 0; i < randomNumbers.length; i++) {
        randomNumbers[i] = RND_GEN.nextInt(10) + 1;
    }
    return randomNumbers;
}

public void printNum(int[] randomNumbers){
    for (int i = 0; i < randomNumbers.length; i++) {
        System.out.println("Number " + i + " : " + randomNumbers[i]);
    }
    System.out.println("Single # is: " + (RND_GEN.nextInt(10) + 1)); 
}

public void searchArray(int[] randomNumbers,int numSearch) {
int count = 0;
for (int i : randomNumbers) {
    if (i == numSearch) {
        count++;
    }
}
if (count == 0) {
    System.out.println("Single #" + numSearch + " was not found!");
} else {
    System.out.println("Single # "+ numSearch + " was found " + count + "   times");
}
}




public void run() {
    Scanner inputReader = new Scanner(System.in);
    int x = 1;
    do {
        int[] numbers = new int[20];
        numbers = createNum(numbers);
        printNum(numbers);
        int numSearch = RND_GEN.nextInt(10) + 1;
        searchArray(numbers, numSearch);
        System.out.print("Restart Program?, Enter 1 for YES, 2 for NO: ");
        x = inputReader.nextInt();
    } while (x == 1);
}

public static void main(String[] args) {
    Main go = new Main();
    go.run();
}
}

Solution

  • You're almost there. You've already done the work of creating a search method, you're just not calling it. Modify your searchArray method to print out the number of occurrences at the end since it's a requirement that you do.

    public void searchArray(int[] randomNumbers, int numSearch) {
        int count = 0;
    
        for (int i : randomNumbers) {
            if (i == numSearch) {
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Single #" + numSearch + " was not found!");
        } else {
            System.out.println("Single # "+ numSearch + " was found " + count + " times");
        }
    }
    

    You can then call it like so:

    searchArray(numbers, numSearch);