Sarah Mathews
Sarah Mathews

Reputation: 21

Compiler errors for If statement

I am trying to implement part of a program that will convert a DD/MM/YY input into a DD/MM/YYYY input. I have used an if statement to ask the program to check whether the input number is <13, and if so, to add 2000 to the input number - giving a result of 2012 if 12 is input. If the number is >13 and <100, I have asked the program to add 1900.

I am very new to java and am experiencing two compiler errors which I have been unable to overcome so far (down from 36, haha). The compiler error is "not a statement". Both refer to the line with the else statement. The code is as follows (obviously still a WIP and not a completed program yet) -

import java.util.*;

public class FindDay4Birthdate
{
    public static void main(String[] args)
    {
        String dayInput = "";
        String monthInput = "";
        String yearInput = "";
        int bday;
        int bmonth;
        int byear;

        String daysList[] = {
            "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
        };
        String monthList[] = {
            "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
        };

        Scanner sc = new Scanner(System. in );
        sc.useDelimiter("[-/.\\s]");

        System.out.print("Please enter your date of birth (dd/mm/yyyy) - ");

        if (sc.hasNext())
        {
            dayInput = sc.next();
            monthInput = sc.next();
            yearInput = sc.next();

            bday = Integer.parseInt(dayInput);
            bmonth = Integer.parseInt(monthInput);
            byear = Integer.parseInt(yearInput);


        } // end if statement

        if (byear = (byear > 0));
        {
            if (byear = (byear < 13))
            {
                byear = (byear + 2000);
            }
            else(byear = (byear > 13 && byear < 100));
            {
                byear = (byear + 1900);
            }
        } // end if statement


        bmonth -= 1; //set month to correct array

        String day = daysList[bday];
        String month = monthList[bmonth];

        int yearCount = (byear - 1901);
        int daysInYear = 365;
        int dayCount = (daysInYear * yearCount);

        System.out.println("You were born on " + day + " " + bday + " " + month + " " + byear);

        System.out.println(yearCount + " " + daysInYear + " " + dayCount); // test output only

        /* Todo
         *
         *TASK 2 -
         *Implement daysPerMonth - Feb set to 28 days
         *Use count to determine correct day for birthdate
         *
         *TASK 3 -
         *User input needs to allow char based months and 2 digit years need to be converted to 4 digit years
         *Implement leap years */

    }
}

Upvotes: 0

Views: 188

Answers (3)

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11940

This part is your problem.

if (byear = (byear > 0));
{
    if (byear = (byear < 13))
    {
        byear = (byear + 2000);
    }
    else(byear = (byear > 13 && byear < 100));
    {
        byear = (byear + 1900);
    }
} // end if statement

The first error you made is the semicolon at the end of the if statement.

if (byear = (byear > 0)); // This ; symbol is to be removed

This causes to end the if block without executing any of the statements.

The next error is how you are trying to compare values. Here.

byear = (byear > 0);

This statement assigns the byear variable with true if byear is greater than 0 or else make it false which causes a compiler error since byear is an integer and not a boolean value. So change your first if statement to

if (byear > 0)

See, there is no need to assign it to itself. In the same way removing all the assignments in comparisions make your error part to

if (byear > 0);
{
    if (byear < 13)
    {
        byear = (byear + 2000);
    }
    else(byear > 13 && byear < 100);
    {
        byear = (byear + 1900);
    }
} // end if statement

And now, it should compile.

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

Once you fix the compiler errors, you will have logical errors from this block

    if (byear=(byear >0));{
        if (byear=(byear <13)); 
        {
            byear=(byear + 2000);
        }
        else  (byear =(byear >13 && byear <100)); 
        {
            byear=(byear + 1900);   
        } 
    }  // end if statement

byear=(byear >0) means that byear will now be the result of byear>0 which is either true or false. Now the next conditions no longer make sense.

Change all such byear = <condition> statements in the ifs to just <condition>. i.e (dont copy-paste, understand the difference)

   if (byear >0){
        if (byear <13); 
        {
            byear=(byear + 2000);
        }
        else  (byear >13 && byear <100); 
        {
            byear=(byear + 1900);   
        } 
    }  // end if statement

Upvotes: 1

rajesh
rajesh

Reputation: 3407

Remove the semi colon after your if statement

if (byear=(byear >0)); similarly for following else as well

A semi colon after if will treat it as end of the block. So the else that you have defined is not considered as a part of the if statement that has preceeded it

Upvotes: 4

Related Questions