Willee005
Willee005

Reputation: 21

if and else statements not working as intended

I am making a really simple program, mostly to learn the if/else statements. The program is made to figure out if you have chosen a correct date.

The program is intended to work as follows:

The problem is, that if you type for example 15 in the month section, it states

MonthWrongWrongdayinthemonthWrongdayinthemonth

instead of just

month wrong

Is this because I have if/else statements inside of other if/else statements? I have tried searching for this everywhere but I can not figure out what's wrong..

This is a link to a picture of the console when I try to run the app.

Picture of console

Please excuse the Swedish words.

import java.util.Scanner;

public class DateChecker111 {

    public static void main (String args[]) {

        Scanner scanner1 = new Scanner(System.in);

        int Manad, Dag;
        System.out.print("Ange Månad>");

        Manad = scanner1.nextInt();
        if (Manad > 0 && Manad < 13) {
        }
        else {
            System.out.print("Felaktig Månad");
        }

        if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
            System.out.print("Ange Dag>");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 32); 
            System.out.print("Korrekt Datum");
        }
        else {
                System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 2) {
            System.out.print("Ange Dag");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 29); 
            System.out.print("Korrekt Datum");
        }
        else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) {
            System.out.println("Ange Dag");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 31);
            System.out.print("Korrekt Datum");
        } 
        else {
            System.out.print("Felaktig Dag i Månaden");

            scanner1.close();

        }
    }


}

Upvotes: 0

Views: 239

Answers (8)

Badr Bujbara
Badr Bujbara

Reputation: 8691

You need to put your if(Manad == 2) inside the the else statement that belongs to if(Manad == 1..... Also put the if (Manad == 4.... inside the else statement of (Manad ==2). I did some comments and removed some brackets.

    Scanner scanner1 = new Scanner(System.in);

int Manad, Dag;
System.out.print("Ange Månad>");

Manad = scanner1.nextInt();
if (Manad > 0 && Manad < 13) {

    // you're not doing something here
}
else { 
    System.out.print("Felaktig Månad");
}

if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
    System.out.print("Ange Dag>");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 32) {
        System.out.print("Korrekt Datum");
    }
}
else { // called if Manad != 1,3,5,7,8,10, or 12
    System.out.print("Felaktig Dag i Månaden");
}

if (Manad == 2) { // you may wanna put this if else statement inside the previous else
    System.out.print("Ange Dag");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 29) {
        System.out.print("Korrekt Datum");
    }
}
else { // for now, this is called every time Mannad != 2 
    System.out.print("Felaktig Dag i Månaden");
}

if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) { 
    // put this if else statement inside else that belongs to Manad == 2
    System.out.println("Ange Dag");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 31) {
        System.out.print("Korrekt Datum");
    }
}
else {// for now, this is called every time Manad != 4,6,9, or 11
    System.out.print("Felaktig Dag i Månaden");

    scanner1.close();

}

Upvotes: 0

Erick G. Hagstrom
Erick G. Hagstrom

Reputation: 4945

The real problem here, and the root of all your troubles, is that you consistently end if statements with semicolons. For example, you write:

if (Dag > 0 && Dag < 32); 
    System.out.print("Korrekt Datum");

This is equivalent to having no if statement at all!

An if statement has the following form:

if (condition)
    statement

and an if-else statement has the following form:

if (condition)
    statement
else
    anotherStatement

Notice that I left off the semicolons and braces. That was deliberate. In either of those forms, statement and anotherStatement can either be a single statement ending in a semicolon, or a Block consisting of some number of statements inside braces. So when you end your if statement with a semicolon, you're actually writing

if (condition)
    ;

In the example code I quoted above, you're writing

if (Dag > 0 && Dag < 32)
    ; 
System.out.print("Korrekt Datum");

Take out the extra semicolons and your life will be happier.

Upvotes: 2

forgivenson
forgivenson

Reputation: 4435

Below is your code formatted better so you can see the mistakes you've made. There are 3 if statements that end in ;, instead of a { as you intended. With the code formatted as it is now, you should notice that instead of a bunch of nested if statements, you have several if statements that will all be run, even if the first one fails. This is what you are seeing. To fix this, remove the ; at the end of the if statements, and add {. (I marked the bad ifs with // BAD IF)

import java.util.Scanner;

public class DateChecker111 {

    public static void main (String args[]) {

        Scanner scanner1 = new Scanner(System.in);

        int Manad, Dag;
        System.out.print("Ange Månad>");

        Manad = scanner1.nextInt();
        if (Manad > 0 && Manad < 13) {
        }
        else {
            System.out.print("Felaktig Månad");
        }

        if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
            System.out.print("Ange Dag>");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 32);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 2) {
            System.out.print("Ange Dag");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 29);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) {
            System.out.println("Ange Dag");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 31);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");

            scanner1.close();

        }
    }
}

Upvotes: 1

Petter
Petter

Reputation: 1337

You're Gavin said, the execution doesn't stop just because you enter a else statement, it keeps going from that point. If you want it to work as you expect it to you either have to put the code that should run only if the if-statement is true inside the if-block or introduce a return statement.

if(foo == 1) {
  if(bar == 2 {
    //Do something
  }
  else {
    //bar is not 2
  }
}
else {
  //foo is not 1
}

or

if(foo == 1) {
}
else {
  //foo is not 1
  return;
}
if(bar == 2 {
  //Do something
}
else {
  //bar is not 2
  return;
}

Upvotes: 0

SaphirShroom
SaphirShroom

Reputation: 90

Your second if-statement gets executed regardless whether the first one succeeds or not. To fix that, you can add

    return;

right after

    System.out.println("Felaktig Manad");

Also, be very careful with semicolons (;). There are multiple cases where you use a semicolon right after an if-statement which causes the if-statement to do nothing at all (it essentially gets skipped completely). Replace those semicolons with closing curly brackets (}). Depending on your IDE you can probably hit crtl+I so it indents your code automatically and you can see clearly what it does.

Upvotes: 1

shiladitya
shiladitya

Reputation: 2310

Replace the semicolon with bracket in this line:

if (Dag > 0 && Dag < 32);

Upvotes: 0

RyanS
RyanS

Reputation: 4194

You have issues with your braces, see this code: if

(Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
        System.out.print("Ange Dag>");
        Dag = scanner1.nextInt();
        if (Dag > 0 && Dag < 32); 
            System.out.print("Korrekt Datum");
            }
            else {
                System.out.print("Felaktig Dag i Månaden");
        }

Your first closing brace } actually closes the main if. The code could be seen as:

if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
    System.out.print("Ange Dag>");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 32); 
        System.out.print("Korrekt Datum");   
}
else {
    System.out.print("Felaktig Dag i Månaden");
}

Upvotes: 0

Gavin
Gavin

Reputation: 1767

The execution flow of the appellation doesn't stop because a branch of the if/else has been executed, it will go on to execute the second if/else the result of which will also be output.

Upvotes: 1

Related Questions