user130875
user130875

Reputation: 93

why i cannot search for my random generate value?

So far i can generate random 20 number that i want...but when i want to Search for my random 20 number ...it alwayS Show "the number you enter cannot found " why ???how do i Search the value from the number i generated ? would be happy if Someone can teach me how to do .

package labtask.pkg5;
import java.util.Scanner;
import java.util.Random;

public class Labtask5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int search = 1;
        int[] array = new int[20];
        Scanner read = new Scanner(System.in);
        Random r = new Random();         //generate random number
        for (int x = 0; x < 20; x++) {
            System.out.println(r.nextInt(100));
        }
        while (search != 0)              //Search for a number
        {
            System.out.println("Search a number");
            search = read.nextInt();
            if (SearchValue(search, array) == true) {
                System.out.println("The number you enter been found");
            } else {
                System.out.println("The number you enter did not found");
            }
        }
    }

    public static boolean SearchValue(int search, int[] array) {
        for (int y = 0; y < 20; y++) {
            if (search == array[y]) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }
}

Upvotes: 1

Views: 51

Answers (2)

Michael
Michael

Reputation: 44230

Your first problem is with this function:

public static boolean SearchValue(int search, int[] array) {
    for (int y = 0; y < 20; y++) {
        if (search == array[y]) {
            return true;
        } else {
            return false;
        }
    }
    return true;
}

You will always return either true or false on the first iteration of the loop and you will never get past y = 0.

Additionally, you do not need to hardcode the value 20. The array is aware of its own size.

I would expect this to look like:

public static boolean SearchValue(int search, int[] array) {
    for (int y = 0; y < array.length; y++) {
        if (search == array[y]) {
            return true;
        }
    }
    return false;
}

Upvotes: 3

1 things:

you are printing 20 random numbers only, but not assigning it into the array:

for (int x = 0; x<20; x++){
    System.out.println(r.nextInt(100));    
} 

do instead:

for (int x = 0;x<20;x++) {
    array[x] = r.nextInt(100);
    System.out.println(array[x]);
} 

on the other hand, the method:

 SearchValue(int search, int[] array)

is returning some boolean immediantly after checking the 1st element in the array, you need to BROWSE the whole array instead...

do instead something like:

public static boolean SearchValue(int search, int[] array) {

    for (int y = 0; y < 20; y++) {
        if (search == array[y]) {
            return true;
        }
    }
    return false;
}

Upvotes: 3

Related Questions