user3388923
user3388923

Reputation: 15

incompatible types in java, using arrays and ints

public class Lab4 {

    public static void main (String args []) {
        int [] myDataList = {12, 223, 232, 434, 1433, 0, -34, 14, 43, 544, 223};
        printArray(myDataList);
        System.out.println();
        addToArrayContents(myDataList,50);
        printArray(myDataList);
        int x = linearSearchForLargest(myDataList);
        System.out.println("The largest number was "+x);
        arrayContainValue(myDataList,23);
    }


    public static void arrayContainValue(int [] ar,int target) {

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

            if (ar[i] = target) {
                System.out.println("The value "+target+" has been found in the array. ");
        }
    }

    System.out.println("The value "+target+" was not found in the array. ");

    }
} 

The line if (ar[i] = target) gives me the incompatible types error when I try to compile. This is just for an assignment but I'm hitting a complete block as to where I've made my error. I've only a few weeks experience with java, so this is all new to me

Upvotes: 0

Views: 60

Answers (1)

Mena
Mena

Reputation: 48404

You're not testing for primitive equality, you're assigning an element of your array at index i with the target value.

Hence your if condition does not test for boolean, and your code won't compile.

Change the line to:

if (ar[i] == target)

Upvotes: 7

Related Questions