Brandon Apter
Brandon Apter

Reputation: 1

issue when creating an "if-else" statement

Hi i'm pretty new to java I've been having an issue when I attempt an "if-else" statement. I get syntax errors, which I'm not sure why. Thanks for your input.

 import javax.swing.JOptionPane;

public class MagicDate {

    public static void main(String [] args){

        int month;
        int day;
        int year;
        String input;
        String message = ("This program will find a secret date");

        JOptionPane.showMessageDialog(null, message);

        input = JOptionPane.showInputDialog( " Enter a month of the in numeric "
                                            + "form");
        month= Integer.parseInt(input);

        input = JOptionPane.showInputDialog( " Enter a day of the in numeric"
                                            + " form");
        day= Integer.parseInt(input);

        input = JOptionPane.showInputDialog( " Enter a two digit year");
        year = Integer.parseInt(input);

        if(month * day == year);
        {
            JOptionPane.showMessageDialog(null, "The Date is Magic!");
        }
        else 
        {
            (month * day != year);
            JOptionPane.showMessageDialog(null, "The Date is not Magic");
        }   
        System.exit(0);

    }
}

Upvotes: 0

Views: 33

Answers (3)

Himanshu
Himanshu

Reputation: 4395

Remove ; after if condition

if(month * day == year); // remove ;

and missing if

else 
{
    (month * day != year); // add if before line and remove ;
    JOptionPane.showMessageDialog(null, "The Date is not Magic");
}

Upvotes: 0

mucio
mucio

Reputation: 7119

Remove the semicolon:

if(month * day == year)

Upvotes: 0

amahfouz
amahfouz

Reputation: 2398

You should remove the semicolon at the end of the line containing the if statement. Basically, the effect of the semicolon is adding an empty statement. So your code reads like this to the compiler:

If (...)
   Do nothing;
// can't have an brace here since the if statement has already terminated.
{
   ...
}

Upvotes: 2

Related Questions