harwind

harwind

Java's for each loop

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:

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:

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, 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!

Most Liked

dtonhofer

dtonhofer

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

Isn’t the JUnit test failing because the

hasApplicationNumber(1)

throws?

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

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

etc.

finner

finner

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.

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:

bad number
java.lang.IllegalArgumentException: bad number
. . . 
finner

finner

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

Popular General Dev topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
justinjunodev
Figured this would be a cool topic and maybe provide some inspiration for those who are just starting to work from home. Feel free to sha...
New
AstonJ
Perhaps we can add some vids on sound differences, those I’ve seen so far are giving the plastic cases a nice stock sound (you could do s...
New
AstonJ
Inspired by some of the comments in our https://forum.devtalk.com/t/your-vim-tips/4748 (in particular those by Mafinar and Hallski) …what...
New
PragmaticBookshelf
Craft your dream role at work by guiding your manager to take your priorities into account when making decisions. Ken Kousen @kenko...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
DevotionGeo
I installed Github Copilot (VS Code extension) and signed up for the technical preview three days ago. Yesterday I got the invitation, an...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
CommunityNews
Christian Mills - Testing Intel’s Arc A770 GPU for Deep Learning Pt. 2. This post covers my experience training image classification mod...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New