0

I wanna know where I went wrong with my logic. I want to entire the for loop when found == true and do the condition else if not found i wanna execute that condition. Any help would be greatly appreciated..

boolean found = false;

for (String keyword : keywords) {
    found = true;
    if (input.contains(keyword)) {
        parseFile(keyword);
        break;
    }
    if (!found) {
        Writer();
        break;
    }
}
4
  • 4
    I cannot understand what you want Commented Apr 14, 2015 at 4:32
  • Why do you make found = true; for each keyword. This will stay true for all the cases. Commented Apr 14, 2015 at 4:34
  • I do not understand. You're saying I should set found = true instead of found = false ? Commented Apr 14, 2015 at 4:36
  • No @Eggz you must change that by a method. By checking. Otherwise it will stay the same for all the cases. Commented Apr 14, 2015 at 4:37

4 Answers 4

3

I'm not entirely sure if this is correct, but it sounds like you want to loop through your keywords and if the keyword is found, you want to run your first condition and leave the for loop. Then if none of the keywords match, you want to do the final condition. If so, this should match what you are looking for:

boolean found = false;
for (String keyword : keywords) 
{
    if (input.contains(keyword)) 
    {
        found = true;
        parseFile(keyword);
        break;
    }
}
if (!found) 
{
    Writer();
}
Sign up to request clarification or add additional context in comments.

Comments

2

I'm pretty sure you want to change found based on the if test in the loop. Like,

// found = true;
if (input.contains(keyword)) {
    found = true; // <-- otherwise it doesn't make sense.

since you then test

if (!found) { // <-- can only evaluate to true if found is false.

Also, I think that's supposed to be after you loop. Something like,

for (String keyword : keywords) {
    if (input.contains(keyword)) {
        found = true;
        parseFile(keyword);
        break;
    }
}
if (!found) {
    Writer();
}

3 Comments

for (String keyword : keywords) { if (input.contains(keyword)) { found = true; parseFile(keyword); break; } } if (!found) { Writer(); break; }
@PrashantKhaire No break needed outside the for loop.
if the keyword is not found the second if statement is skipped. I do not know for what reason
1

You are setting found = true outside the first if statement, so you will never enter the if (!found) block. Instead, you might want to set it up like this:

boolean found = false;
for (String keyword : keywords) {
    if (input.contains(keyword)) {
        found = true;
        parseFile(keyword);
        break;
    } 
}

if (!found) {
    Writer(); 
}

Comments

0

By setting found = true; outside of your if statement, you erase any chance of found being false.

In other words, you've set found to true and nowhere in your code do you create a condition where found switches to false, so it will stay true This makes your entire if (!found) block is useless.

You need to set found to true outside the for loop

Comments

Your Answer

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