×

java · level 1 · unit 6 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Loop through an array to find what you need.

To an array, look at each item with a loop and an if. Before the loop, make a : a boolean box holding false. When you find a match, raise the flag by setting it to true.

The same trick finds the biggest item. Keep the first item in a box called best. Each time you meet a bigger one, put that in best instead.

For text items, compare with .equals, not ==.

public class Main {
    public static void main(String[] args) {
        String[] fridge = {"milk", "cheese", "jam"};
        boolean found = false;
        for (String food : fridge) {
            if (food.equals("cheese")) {
                found = true;
            }
        }
        System.out.println("Cheese in the fridge: " + found);
    }
}

Once the flag is up, break can stop the search early.