Ming Hong
Ming Hong

Reputation: 3

The searchValue method array come out all is 0

The part in the searchValue method array come out all is 0, I tested the randomFill method on other class come out the value is not all 0, maybe there is some error in the way I pass those array to other method.

public class test1 {

    //variable to use in other method
    private static int anArray[] = new int[20];
    private static int userNumber[] = new int[20];

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner scanner = new Scanner (System.in);

        //get number to check in array
        for (int i = 0; i < userNumber.length; i++) {
            System.out.print("Enter number to check " + (i+1) + " : ");
            userNumber[i] = scanner.nextInt();
        }

        //print out final result
        searchValue(anArray, userNumber);

    }

    // generate random number
    public int[] randomFill(){
          for(int i = 0; i < anArray.length; i++){ 
              anArray[i] = (int)(Math.random() * (20-1)+ 1);
          }

          return anArray;
        }

    //compare to the array does the input number match
    public static int[] searchValue(int[] randomFill, int[] userNumber){
        for(int i = 0; i < anArray.length; i++){
            if(anArray[i] == userNumber[i]){
                System.out.println("Number exist ");
                return anArray;
            }else{
                // this is to check my array number, find out all is 0, will remove is once is done
                System.out.println(anArray[i]);
            }
          }
        System.out.println("Number do not exist ");
        return anArray;
        }
}

Upvotes: 0

Views: 163

Answers (3)

Thomas
Thomas

Reputation: 111

You never called the method "randomFill". Since you have two arrays declared in the class, you don't need any parameters and returns. Just call the mehtod randomFill after filling your userinput array. If you want to check if an array contains any value of an other array, you need to change your algorithm in searchValue() to iterate through one array and check every position in the other one.

    import java.util.Scanner;

    public class test1 {

        // variable to use in other method
        private static int anArray[] = new int[20];
        private static int userNumber[] = new int[20];

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            Scanner scanner = new Scanner(System.in);

            // get number to check in array
            for (int i = 0; i < userNumber.length; i++) {
                System.out.print("Enter number to check " + (i + 1) + " :                       ");
                userNumber[i] = scanner.nextInt();
            }
            randomFill();
            searchValue();
        }

        // generate random number
        public static void randomFill() {

            for (int i = 0; i < 20; i++) {
                anArray[i] = (int) (Math.random() * (20 - 1) + 1);
            }
        }

        // compare to the array does the input number match
        public static void searchValue() {
            for (int i = 0; i < userNumber.length; i++) {
                for (int j = 0; j < anArray.length; j++) {
                    if (userNumber[i] == anArray[j]) {
                System.out.println("Usernumber " + userNumber[i] + " exists at  ArrayIndex :" + j);
                    }
                }
            }
        }
    }

This should return every duplicate.

Upvotes: 0

Rogue
Rogue

Reputation: 11483

You never called #randomFill, so the array values are never randomized. Additionally, there's some "code smells" in your code, which is that you're passing arrays as parameters but still simply mutating a pseudo-global value.

Technically, you wouldn't need to return the array either, since an array can be modified by a method you pass it to (java's pass-by-value-of-reference). For example:

public static void randomFill(int[] myArray) { //note the added parameter
    for(int i = 0; i < myArray.length; i++){ 
        myArray[i] = (int)(Math.random() * (20-1)+ 1);
    }
}

//Elsewheres
int[] myAwesomeArray = new int[20];
randomFill(myAwesomeArray);
//myAwesomeArray is now randomized

I would also check very carefully over your printing/debug method (as Shriram pointed out). But that's not quite the original question.

Upvotes: 3

Jay Smith
Jay Smith

Reputation: 2480

You have to call method randomFill before searching it. When array is created it is filled with 0.

But to call method randomFill you have to make it static. Correct code:

package com.stackoverflow.main;

import java.util.Scanner;

public class test1 {

    // variable to use in other method
    private static int anArray[] = new int[20];
    private static int userNumber[] = new int[20];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        randomFill();
        Scanner scanner = new Scanner(System.in);

        // get number to check in array
        for (int i = 0; i < userNumber.length; i++) {
            System.out.print("Enter number to check " + (i + 1) + " : ");
            userNumber[i] = scanner.nextInt();
        }

        // print out final result
        searchValue(anArray, userNumber);

    }

    // generate random number
    public static int[] randomFill() {

        for (int i = 0; i < anArray.length; i++) {

            anArray[i] = (int) (Math.random() * (20 - 1) + 1);
        }

        return anArray;

    }

    // compare to the array does the input number match
    public static int[] searchValue(int[] randomFill, int[] userNumber) {

        for (int i = 0; i < anArray.length; i++) {

            if (anArray[i] == userNumber[i]) {

                System.out.println("Number exist ");

                return anArray;

            } else {
                // this is to check my array number, find out all is 0, will
                // remove is once is done
                System.out.println(anArray[i]);
            }

        }
        System.out.println("Number do not exist ");

        return anArray;

    }

}

Upvotes: 0

Related Questions