Reputation:
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;
}
}
Upvotes: 0
Views: 4108
Reputation: 1
By setting found = true;
outside of your if
statement, you erase any chance of found
being false
.
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
Upvotes: 0
Reputation: 201409
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();
}
Upvotes: 2
Reputation: 301
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();
}
Upvotes: 1
Reputation: 500
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();
}
Upvotes: 3