0

I am trying to understand why I cannot print my desired outcome. I have changed my loop logic around a few times but cannot see the issue.

I have an array list ArrayList<Landing> landings = new ArrayList<>();

I have the following method in the Landing class to check the landing ID;

public boolean checkId(int id) {
    if (this.landingId == id) {
        return true;

    } else {
        return false;
    }
}

I am trying to create some input handling where if the user inputs an ID which doesn't match an ID in the array list, an error message is printed. I can't seem to get the failed scenario to print the message

System.out.print("Please enter the landing ID: ");
            int id = Integer.parseInt(sc.nextLine());
            boolean unsuccessful = false;
            for (int i = 0; i < landings.size(); i++) {

                if (landings.get(i).checkId(id)) {
                    landings.get(i).printLandings();
                    unsuccessful = false;
                    break;
                } else {
                    unsuccessful = true;

                }
            }

            if (unsuccessful) {
                System.out.println(
                        "\nThe ID you enterred does not match any landing");

            }
break;

TIA

3
  • break in the if branch will just end at the first successful condition. You should break when you set unsuccessful to true, instead. Otherwise the next successful condition will just reset the boolean, break the loop and skip the message. Commented Aug 21, 2021 at 9:41
  • Unrelated, but checkId can be simplified to return this.landingId == id; Commented Aug 21, 2021 at 9:42
  • @Federico klez Culloca That is the desired outcome for a successful ID match. However, if it fails, it needs to print the message once Commented Aug 21, 2021 at 9:44

2 Answers 2

1

You can do the following if you use Java 8 or above,

Optional<Landing> landing = landings.stream().filter(i -> i.checkId(id)).findFirst();
landing.ifPresent(Landing::printLandings);
boolean unsuccessful = !landing.isPresent();

Also you can improve checkId method as follows,

public boolean checkId(int id) {
        return this.landingId == id;
    }

Hope this will sort out your issues. cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to check if there's "any" (= at least one) match, you gotta start with boolean unsuccessful = true.

Let's look at the simplest case: When the List is empty, the loop won't run a single time. Though unsuccessful will not be changed and has to set to true before the loop.

Here's a simplified version.

boolean unsuccessful = true
for(...) {
  if(matchFound) {
    unsuccessful = false;
    break;
  }
}

if(unsuccessful) { print(...); }

Maybe you should also consider renaming the variable to success (at least for me that's easier to read):

boolean success = false;
for(...) {
  if(matchFound) {
    success = true;
    break;
  }
}

if(!success) { print(...); }

As you can see, you also don't need the else part anymore.

1 Comment

Legend! Thanks @Benjamin M for the explanation and helping simplify. Working well now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.