Results 1 to 15 of 15
  1. #1
    BangingWang is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default LeapYear program crashes.

    Code:
    import java.util.Scanner;

    public class Leap_Year
    { public static void main(String[] args)
    { Scanner in = new Scanner (System.in);
    System.out.println("Imput your year");


    int year = in.nextInt();
    int leapyear1 = year % 4;
    int leapyear2 = year % 400;
    int leapyear3 = year % 100;
    if (leapyear2 == 0)
    System.out.println("It is a leapyear");
    else if (leapyear3 == 0)
    System.out.println("It is not a leapyear");
    else if (leapyear1 == 0)
    System.out.println("It is not a leapyear");


    }
    }

    So, I am trying to create a program that will tell whether or not the inputted number is a leap year or not, but my program crashes when a number like 567 is inputted. What is wrong with my code?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    When you get errors copy and paste the full and exact message here. Also indicate on which line the error occurs.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Works for me...

    Java Code:
    C:\>type LeapYear.java
    import java.util.Scanner;
    
    public class LeapYear {
            public static void main(String[] args) {
                    Scanner in = new Scanner(System.in);
                    System.out.println("Imput your year");
    
                    int year = in.nextInt();
                    int leapyear1 = year % 4;
                    int leapyear2 = year % 400;
                    int leapyear3 = year % 100;
    
                    if(leapyear2 == 0) {
                            System.out.println("It is a leapyear");
                    } else if(leapyear3 == 0) {
                            System.out.println("It is not a leapyear");
                    } else if(leapyear1 == 0) {
                            System.out.println("It is not a leapyear");
                    }
            }
    }
    
    C:\>javac -cp . LeapYear.java
    
    C:\>java -cp . LeapYear
    Imput your year
    567
    
    C:\>
    There is no output because for 567 leapyear1 is 3, leapyear2 is 167 and leapyear3 is 67 ie none of them ==0.

    I'm mostly posting to illustrate a more readable style of formatting. See, for example, the Java Code Conventions.

  4. #4
    agentx64 is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    first you need to use proper if else block by use curly bracket...
    second your modulus equation it wrong...

    here my code
    Java Code:
    import java.io.*;
    class example
    {
    	public static void main(String[] args) 
    	throws  Exception
    		{
    		BufferedReader tempS = new BufferedReader (new InputStreamReader(System.in));
    		
    		int year=0;
    		
    		System.out.print ("Input your year :");
    		year=Integer.parseInt(tempS.readLine());
    		
    			if(year % 4==0)
    			{
    				System.out.println(year+" is a leap year");
    			}
    			else
    			{
    				System.out.println(year+" Is not a leap year");
    			}
    		}
    }
    output
    Input your year :2011
    2011 Is not a leap year
    Last edited by agentx64; 08-26-2011 at 09:16 AM. Reason: put output for my code

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @agent: In fact your use of % is also not in line with what is commonly meant by a "leap year". But more to the point, shouldn't we let the OP discover both the problems with his or her code, and the way to express those problems to others?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    There is no output because for 567 leapyear1 is 3, leapyear2 is 167 and leapyear3 is 67 ie none of them ==0.
    Maybe the year 567 was a leap year in those days, who knows? Pope Gregory invented the current mess a millenium later and in those old days there was a complete anarchy. Heck, every year could've been a leap year in those days ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication1;

    /**
    *
    * @author vinil.mehta
    */
    public class LeapYear {
    int LeapYear;//instance variable

    public LeapYear(int num) //constructor
    {
    LeapYear=num;
    }
    public void show()//methods
    {
    if(LeapYear%4==0 && LeapYear%100==0 && LeapYear%400==0)
    {
    System.out.println("Leap Year");
    }
    else{
    System.out.println("Enter some other year !");
    }
    }
    {
    System.out.println(LeapYear);
    }
    public static void main(String args[])
    {
    LeapYear lp=new LeapYear(2000) ;
    lp.show();
    }
    }
    This code is running

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vinci123 View Post
    Java Code:
            if(LeapYear%4==0 && LeapYear%100==0 && LeapYear%400==0)
            {
        System.out.println("Leap Year");
            }
            else{
            System.out.println("Enter some other year !");
            }
    This code is running
    Your code might be doing something but it isn't doing the correct and wanted thing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default

    here ,in this code even 567 runs .the need is that previous code crashes .Now see this one doesn't crashes at all.What is the basic need if i am not correct ?

  10. #10
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default

    and i am sorry to say but that one code is running too . their is no problem at all with the code posted by bangingwang

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vinci123 View Post
    What is the basic need if i am not correct ?
    Type, say, 1984 (it definitely is a leap year).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default

    I am unable to find which type you are talking about ?describe the problem .

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vinci123 View Post
    I am unable to find which type you are talking about ?describe the problem .
    Your program gives incorrect answers; e.g. 1984 is definitely a leap year but your program says it isn't.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication1;

    /**
    *
    * @author vinil.mehta
    */
    public class LeapYear {
    int LeapYear;//instance variable

    public LeapYear(int num) //constructor
    {
    LeapYear=num;
    }
    public void show()//methods
    {
    if((LeapYear%4==0 && LeapYear%100!=0) || LeapYear%400==0)
    {
    System.out.println("Leap Year");
    }
    else{
    System.out.println("Enter some other year !");
    }
    }
    {
    System.out.println(LeapYear);
    }
    public static void main(String args[])
    {
    LeapYear lp=new LeapYear(1984) ;
    lp.show();
    }
    }
    hope you find this useful

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vinci123 View Post
    Java Code:
            if((LeapYear%4==0 && LeapYear%100!=0) || LeapYear%400==0)
            {
        System.out.println("Leap Year");
            }
            else{
            System.out.println("Enter some other year !");
            }
    hope you find this useful
    That's better; b.t.w. your naming conventions are confuscating and not according to what Sun/Oracle like.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Weird crashes...
    By illnin0 in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 03:04 AM
  2. Program crashes: NullPointerException
    By d4nce2thisbeat in forum New To Java
    Replies: 5
    Last Post: 11-20-2010, 09:33 PM
  3. Swing GUI crashes (using JNI)
    By divs in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2010, 08:04 AM
  4. Multithreaded server crashes
    By skarosg3 in forum Threads and Synchronization
    Replies: 7
    Last Post: 05-26-2010, 09:24 AM
  5. Java crashes
    By Nicole in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •