简体   繁体   中英

(Java) If the condition for an if statement inside a for loop is never met, how do I print out a message saying “no values meet this criteria”?

So, I have an if statement inside of a for loop. The idea is, if the time difference between the current time and an updated time is greater than 24 hours (86400000 milliseconds), then I print out the claim number.

This what my if statement looks like:

 if(difference>86400000){                 
    System.out.println(singleClaim.getString("claimNumber"));
        } 

and this is what my output looks like (a list of claim numbers with a certain status that have a time difference of over 24 hours):

 032394115-01
 032398720-01
 032395941-01
 032398165-01
 032395262-01
 032395350-01
 032392831-01

Now, if there aren't any claim numbers with a certain status that have a time difference of over 24 hours, I want my output to look like this:

No claim numbers meet this criteria.

How would I add that in there?

I tried doing this:

if(difference>86400000){                 
    System.out.println(singleClaim.getString("claimNumber"));
        } 
else{
 System.out.println("No claim numbers meet this criteria.");
 }

and changed the data to make sure no claim numbers had a difference greater than 24 hours but this is what I got as my output (the message displayed over and over again instead of the claim numbers):

No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.
No claim numbers meet this criteria.

You would have to create a flag to tell if a claim has met the conditions. So outside your loop do something like:

boolean claimMet = false;

and in the if-statement:

if(difference>86400000){                 
    System.out.println(singleClaim.getString("claimNumber"));
    claimMet = true;
}

then after the loop ends:

 if (!claimMet) {
     System.out.println("No claim numbers meet this criteria.");
 }

I'm not sure I understood what you said, but let's try to answer it if I get it right :

boolean meetCriteria = false;

for(int difference = 0; ; difference++) {
    if(difference>86400000){                 
        meetCriteria = true;
    } 
} 

if(meetCriteria) {
    System.out.println(singleClaim.getString("claimNumber"));
} else {
    System.out.println("No claim numbers meet this criteria.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM