Hey guys,

I need some help again with this program I've been working on. Basically I'm trying to make it so that a user enters a year and then program prints if its a leap year or not.

Here is what I have so far:

// LeapYear.java
// Name


public class LeapYear
// Type in a year
// Print out whether year is leap year or not

{    
    import Java.util.Scanner;
    public static void main( String[] args)
    {
    scanner scan = new Scanner(system.in);
        
    System.out.println("Enter the year: " );
    year = scan.nextInt();
    
    // calculate whether or not year is a leap year
    
     boolean leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

    
    if (isLeapYear)
        {
            println(year + " is a leap year.");
        } else
            println(year + " is not a leap year.");
    }

}

Here are the errors when compiled:

LeapYear.java:10: illegal start of type
import Java.util.Scanner;
^
LeapYear.java:10: ';' expected
import Java.util.Scanner;
^
LeapYear.java:10: illegal start of type
import Java.util.Scanner;
^
LeapYear.java:10: ';' expected
import Java.util.Scanner;
^
LeapYear.java:10: <identifier> expected
import Java.util.Scanner;

Any help is appreciated!

You cannot import inside a class. Move the import statement all the way to the top, above your class declaration. Import statements should not be incased in any code, declare them above your code at all times.

Moved it, but now I get these errors.

LeapYear.java:5: package Java.util does not exist
import Java.util.Scanner;
^
LeapYear.java:13: cannot find symbol
symbol : class scanner
location: class LeapYear
scanner scan = new Scanner(system.in);
^
LeapYear.java:13: cannot find symbol
symbol : class Scanner
location: class LeapYear
scanner scan = new Scanner(system.in);
^
LeapYear.java:13: cannot find symbol
symbol : variable system
location: class LeapYear
scanner scan = new Scanner(system.in);
^
LeapYear.java:16: cannot find symbol
symbol : variable year
location: class LeapYear
year = scan.nextInt();
^
LeapYear.java:20: cannot find symbol
symbol : variable year
location: class LeapYear
boolean leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
^
LeapYear.java:20: cannot find symbol
symbol : variable year
location: class LeapYear
boolean leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
^
LeapYear.java:20: cannot find symbol
symbol : variable year
location: class LeapYear
boolean leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
^
LeapYear.java:23: cannot find symbol
symbol : variable isLeapYear
location: class LeapYear
if (isLeapYear)
^
LeapYear.java:25: cannot find symbol
symbol : variable year
location: class LeapYear
println(year + " is a leap year.");
^
LeapYear.java:25: cannot find symbol
symbol : method println(java.lang.String)
location: class LeapYear
println(year + " is a leap year.");
^
LeapYear.java:27: cannot find symbol
symbol : variable year
location: class LeapYear
println(year + " is not a leap year.");
^
LeapYear.java:27: cannot find symbol
symbol : method println(java.lang.String)
location: class LeapYear
println(year + " is not a leap year.");
^
13 errors

scanner scan = new Scanner(system.in);

1.change scanner to Scanner. Change system.in should be System.in

2. import statement should be - import java.util.Scanner; //not Java.util.Scanner;

3. You haven't declared a type for 'year'. You need to make it an int.

4. There is no boolean called isLeapYear. It is just leapyear

5. To print in java, you need the System.out.println("hi"); //println("message"); will not work


Question: Are you new to java? How are you learning it?

The errors I have found are indicated as follows:

(1) line 10 is a header declaration (import java.util.*;", which should be place in "the heading part", i.e. the first line of the code.
(2) line 13:
The class name is Scanner rather than scanner;
(3) lines 25,27:
the method println(...) are missing handles. Both should be:
System.out.println(...);
(4) line 16 the variable year has not been delrared yet. The line 16 should be:
int year = scan.nextInt();
(5) In line 20 the boolean variable's name (leapyear ) is not consistent with the later name (isLeapYear) in line 23. Both should be identical.

Hey guys,

Cheers for the assistance appreciate it.

The program is compiled but still is not running properly.

Here is the code:

// LeapYear.java
// Name

import java.util.Scanner;
public class LeapYear
// Type in a year
// Print out whether year is leap year or not

{
    public static void main( String[] args)
    {
    Scanner scan = new Scanner(System.in);
    
    int year = scan.nextInt();
    boolean leapyear;
    
    System.out.println("Enter the year: ");
    year = scan.nextInt();
    
    // calculate whether or not year is a leap year
    
    leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

    if (leapyear)
    {
    System.out.println(year + " is a leap year.");
    
    } else

    System.out.println(year + " is not a leap year.");
        
    }

}

It's just running endlessly :3

NVM one sec!

huh?

^ I noticed an error, which I fixed. It can compile completely but its still not running properly. :3

The program is running endlessly and the prompts are being displayed. Not sure whats wrong with it.

// LeapYear.java
// Name

import java.util.Scanner;
public class LeapYear
// Type in a year
// Print out whether year is leap year or not

{
    public static void main( String[] args)
    {
    Scanner scan = new Scanner(System.in);
    
    int year = scan.nextInt();
    boolean leapyear;
    
    System.out.println("Enter the year: ");
    year = scan.nextInt();
    
    // calculate whether or not year is a leap year
    
    leapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

    if (leapyear)
    {
    System.out.println(year + " is a leap year.");
    
    } else

    System.out.println(year + " is not a leap year.");
        
    }

}

Should replace
line 14: int year = scan.nextInt(); with the following code:

int year; // declaration of the integer variable year

Thanks you guys! It works now! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.