Home > front end >  i need help figuring out how to use contain properly
i need help figuring out how to use contain properly

Time:10-11

hello i wanted to know why contain is not working and how to make it work. my Goal is to be able to print out false if it has 2 or 3. i am not sure if i am doing it correctly or if i'm on the right track.

 public class Assignment7 {
            public static void main(String[] args) {
                        int[] array0 = {3, 5, 6};
                System.out.println("Your implementation of no23 method printed out: "   no23(array0) );
        }
    public static void no23(boolean array0){
           int number = 2;
           int numberB = 3;
           int theNumbers = number & numberB;
        
           boolean statemnt = array0.contains(theNumbers);
        }
        }

CodePudding user response:

You have multiple problems.

Arrays have no contains method

array0.contains(theNumbers)

You cannot call contains on an array. Arrays have no such method.

There are several ways to search an array in Java. If your array is sorted, use Arrays.binarySearch.

Ampersand (&) flips bits

Your use of & is not doing what you think. That operator manipulates bits. See The Java Tutorials by Oracle.com, free of cost.

What you meant to do was create a collection of int values. To do that use an array or use a Collection object such as List or Set. See The Java Tutorials by Oracle to learn about the Java Collections Framework.

Solution

Use a pair of vertical bars for a logical OR operator. Returns true if either or both conditions are true. See The Java Tutorials by Oracle.

int[] arr = { 3, 5, 6 } ;
int x = 2 ;
int y = 3 ;
boolean arrayContainsEitherNumber = 
    ( Arrays.binarySearch( arr , x ) < 0 ) 
    ||
    ( Arrays.binarySearch( arr , y ) < 0 )
;

Get fancy with streams.

int[] arr = { 3 , 5 , 6 };
boolean hit =
        Arrays
                .stream( arr )                            // Returns an `IntStream`, a series of each `int` value in the array.
                .filter( i -> ( i == 2 ) || ( i == 3 ) )  // Skips any elements not meeting our criteria.
                .findAny()                                // Halts the stream when first element reaches this point.
                .isPresent();                             // Returns true if the payload of this optional exists, false if no payload.
System.out.println( "hit = "   hit );

hit = true

Declare a return type

As commented by Dawood ibn Kareem, your method neglected to return the result to the calling code.

Change the void:

public static void no23( boolean array0 ) { … }

… to boolean and return your result:

public static boolean no23( boolean array0 ) { 
    … 
    return statemnt ; 
}

You may detect a theme in this Answer: Study The Java Tutorials by Oracle before posting here.

CodePudding user response:

I don't understand your question very well, but theNumbers is always equal to 00000010 & 00000011 = 00000010 = 2.

& operator is not the same as the && operator. The first one takes two numbers in binary form, and performs an AND operation like shown above.

2 in binary = 00000010

3 in binary = 00000011

2 & 3 is 00000010 & 00000011 which is 00000010

The second takes two booleans, and returns true if both are true, else returns false.

Effectively, your function is just checking if your array contains the number 2. If you want to check if there is either the number one (1) or the number two (2), you need to do :

public static void no23(int[] array0){ // Also, should be an array      
    boolean containsTwoOrThree = array0.contains(2) || array0.contains(3);
    System.out.println(containsTwoOrThree); // Will print either true or false
    if(containsTwoOrThree) System.out.println("Contains two or three");
    else System.out.println("Doesn't contain");
    // Simple java arrays do not have a "contain" method.
    // Either use Java Lists, or use a for loop to check for values in.
}

EDIT: Arrays in java don't have a contains function. Check this answer: How do I determine whether an array contains a particular value in Java?

  • Related