issue in my if statement to make comparison in my java program

130 views Asked by At

any help please, so i already wrote the prog but my if statement in my for loop is not working. the prog need to generate 6 random nos,then apply bubble sort which i already did.then the user must enter 6 numbers and these numbers must be compared against the random numbers and must say whether numbers are found in the random numbers or not. here's the code. something is wrong with the if statement ` public static void main(String[] args) { try {

        int numbers[] = new int[6];  //random numbers will be stored in new array
        //2 loop will be created to avoid duplication of numbers 
        System.out.println("Array before Bubble sort");
        for (int i = 0; i < 6; i++) {
            numbers[i] = (int) (Math.random() * 40);
            if (i > 0) {
                for (int b = 0; b < i; b++) {  //
                    if (numbers[b] == numbers[i]) {
                        i--;  //decrement to continue the for loop if the integer has been repeated
                    }
                }
            }

            System.out.print(numbers[i] + ",");  //random numbers will be printed before using sorting bubble sort

        }
        //sort an array using bubble sort
        bubbleSort(numbers);
        System.out.println("    \nArray after bubble sort");
        for (int i = 0; i < 6; i++) {
            System.out.print(numbers[i] + ",");
        }
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("\ninput 6 number between 1 and 40");
        int inputNumber = Integer.parseInt(input.readLine());
        for (int b = 0; b < 6; b++) {
            System.out.println("number:");
            int outcome=Integer.parseInt(input.readLine());
            if(outcome==numbers){
                System.out.println("found in random numbers");
            }else{
                System.out.println("not found in random numbers");
            }
        }

    } catch (Exception e) {
        System.out.println("error");
    }

}

public static void bubbleSort(int[] numbers) {

    int n = numbers.length;
    int temp = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {
            if (numbers[j - 1] > numbers[j]) {  //swap the element
                temp = numbers[j - 1];
                numbers[j - 1] = numbers[j];
                numbers[j] = temp;

            }
        }
    }

}

}`

1

There are 1 answers

0
A Diss On
System.out.println("\ninput 6 number between 1 and 40");
        //Scanner is specifically designed for getting an input purpose and introduced in Java 5,so better use it
        Scanner s = new Scanner(System.in);

        //you need to have nested loop here
        //But the best way to search is use binary search,as you have already sorted the array
        while (s.hasNextInt()) {
            //one at a time from the input is put to outcome
            int outcome = s.nextInt();
            boolean found = false;

            for (int b = 0; b < 6; b++) {


                found = false;

                if (outcome == numbers[b]) {
                    found = true;
                    //remember to break the inner loop if a match is found
                    break;
                } else {

                    found = false;
                }

            }
            if (found == true) {
                System.out.println("found in random numbers");
            } else {
                System.out.println("not found in random numbers");
            }