简体   繁体   中英

if else-if not working

I have an if else-if block as follows but it works only half-way.The if part works and so does the first if-else. But the last two if-else statements are never getting executed even in cases when they should evaluate to true. Can someone tell me what am I doing wrong here?

//Getting current date and time
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);                     
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

//Getting user-entered date and time values.
String[] selected_date_string = date.getText().toString().split("-");
int selected_month = Integer.parseInt(selected_date_string[0]);
int selected_day = Integer.parseInt(selected_date_string[1]);
int selected_year = Integer.parseInt(selected_date_string[2]);
String[] selected_time_string = time.getText().toString().split(":");
int selected_hour = Integer.parseInt(selected_time_string[0]);
int selected_minute = Integer.parseInt(selected_time_string[1]);

boolean has_error = false;
int error = -1;

//PROBLEM AREA
if (selected_year < year) {
    error = 1;
} else if ((selected_year == year) && (selected_month < month)) {
    error = 2;
} else if ((selected_year == year) && (selected_month == month)) {//this part doesnt work no matter what I do!
    error = 3;
} else if ((selected_year == year) && (selected_month == month) && (selected_day == day)) //this part too!
    if (selected_hour < hour) 
        error = 4;

Thanks in advance!

Usually we forget that MONTH in Calendar is zero based, in other words: the value for january is 0 and not 1 , as we expect..

You may have to decrement your selected_month ...


use this line in your code:

int selected_month = Integer.parseInt(selected_date_string[0]) - 1;

You have a duplicate statement:

else if ((selected_year == year) && (selected_month == month))

The last one will never be evaluated because the previous one will already pick it up. I know the second one is more specific, but that won't matter. For example, if your years match up and your months match up, it won't matter that your days do too because the first statement that becomes true is your error = 3 else if statement.

As for why neither of the bottom two statements get evaluated, I believe @Andreas_D is onto it when he mentions that the month is a zero-based value. It may be that the statement is working, just not as you expected.

Calender class in Java returns 0-11 for month,

so if current month is Jan then it returns 0

for feb 1 as so on,

for December it returns 11

so your if condition never equals any way

check it

comment or reply if any issue regarding it

You should put the most specific condition as the first if. The last if can't execute as its condition is more specific than the 3rd one ( (selected_year == year) && (selected_month == month) ) but that first part returns true for the 3rd one before the last one get checked, so you'll never get the last if to execute even if it's true for the last part ( selected_day == day ).

Putting that condition as the first one would make sure it will get checked.

Try This.

int month = c.get(Calendar.MONTH)+1;

I would use a switch case construct instead.

As I execute this piece of code:

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

result it:

day = 28
month = 5
year = 2011
hour = 15
minute = 24

So maybe you are passin 06-28-2011 to test it and never got correctly compared, so if I print out error at the end with "07-11-2011" and "18:00" it print -1

When I use it with "05-11-2011" it prints 3

my suggestion is to change

int month = c.get(Calendar.MONTH);

to

int month = c.get(Calendar.MONTH)+1;

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