# Java's for each loop

**URL:** <https://forum.devtalk.com/t/javas-for-each-loop/105241>\
**Category:** General Dev Chat\
**Tags:** java\
**Created:** [16 April 2023 23:31 UTC](https://forum.devtalk.com/t/javas-for-each-loop/105241 "2023-04-16T23:31:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harwind](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@harwind](https://forum.devtalk.com/u/harwind)\
**Post date:** [16 April 2023 23:31 UTC](https://forum.devtalk.com/t/javas-for-each-loop/105241/1 "2023-04-16T23:31:56Z")

</div>

So, basically, I’m writing a method to check application numbers in my array ‘applications,’ and this method receives a parameter to verify that specific application number and compares it to the application numbers in the list.  
So first look at the code:

```auto
public boolean hasApplicationNumber(int number) {
    if (number <= 3 ) {
        throw new IllegalArgumentException("bad number");   
    }

    for (ApplicationData current : applications) {
        if (number == current.getApplicationNumber() ) {    
            return true;
        }
    }
    return false;
}  

```

JUnit test to validate the application number:

```auto
public void shouldHaveApplicationNumber1() {
    University westGeorgia = new University("West Georgia");
    ApplicationData student1 = new ApplicationData(9, 7.0, 400);
    westGeorgia.addApplication(student1);
    assertEquals(true, westGeorgia.hasApplicationNumber(1));
}

```

This JUnit test is failing because my code returns “false” instead of “true.”

However, if I change the return values in my “hasApplicationNumber” method to true (at the very bottom of the method), this test will pass, but another test I have (that doesn’t allow the list to exceed 10) will return “true” when it is supposed to be “false,” causing that test to fail (I didn’t include that test because it is very similar to the one I have already provided — just “1” is changed to “10” and “true”  
I’m beginning to suspect that either my test is being ignored by the Java compiler, or my for-each loop is executing correctly and returning “true,” but since I followed this [documentation](https://www.scaler.com/topics/java/for-each-loop-in-java/), I have that lingering false at the very end, which may be declaring the item false nonetheless.

I might be overthinking this, but I’m at a loss on how to change the for-each loop in the method I wrote. Any assistance in sorting this out would be much appreciated!

---

<div class="post-metadata">

**Author:** ![dtonhofer](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dtonhofer/32/3389_2.png) [@dtonhofer](https://forum.devtalk.com/u/dtonhofer)\
**Post date:** [18 April 2023 08:34 UTC](https://forum.devtalk.com/t/javas-for-each-loop/105241/3 "2023-04-18T08:34:39Z")

</div>

> This JUnit test is failing because my code returns “false” instead of “true.”

Isn’t the JUnit test failing because the

```auto
hasApplicationNumber(1)

```

throws?

At this point, the easiest way to unconfuse yourself is to add a few print statements into the test code.

```auto
System.out.println("After test for throw );

```

etc.

---

<div class="post-metadata">

**Author:** ![finner](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/f/a4c791/32.png) [@finner](https://forum.devtalk.com/u/finner)\
**Post date:** [18 April 2023 08:40 UTC](https://forum.devtalk.com/t/javas-for-each-loop/105241/4 "2023-04-18T08:40:00Z")

</div>

hi @harwind -  
According to the code you have pasted when you pass `1` into the method `hasApplicationNumber` an `IllegalArgumentException` will be thrown. So the test should fail.  
I’ve also noticed that you are using `double` in the `ApplicationData` class but treating them as `int` in the `hasApplicationNumber` method.  
Here is a stripped down version without the `ApplicationData` class and using `int`.

```java
public class DevTalkTest {

    private final int[] numbers = new int[]{9, 7, 400};

    @Test
    public void shouldHaveApplicationNumber1() {
        assertTrue(hasApplicationNumber(1));
    }

    public boolean hasApplicationNumber(int number) {
        if (number <= 3) {
            throw new IllegalArgumentException("bad number");
        }
        for (int num : numbers) {
            if (num == number) return true;
        }
        return false;
    }

}

```

The tests will fail with the error:

```java
bad number
java.lang.IllegalArgumentException: bad number
. . . 

```

---

<div class="post-metadata">

**Author:** ![finner](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/f/a4c791/32.png) [@finner](https://forum.devtalk.com/u/finner)\
**Post date:** [18 April 2023 08:40 UTC](https://forum.devtalk.com/t/javas-for-each-loop/105241/5 "2023-04-18T08:40:51Z")

</div>

I was going to suggest setting a breakpoint on the last line.
