Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Cannot figure out why my code won't work!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Cannot figure out why my code won't work!

    Here is my code that is going to take a user input year and output whether it is a leap year or not.
    A year is a leap year if it is divisible by 4, unless it is divisible by 100 but not 400.
    Also, they year entered has to be after 1582 otherwise it is invalid.

    import java.util.Scanner;
    public class StudentInformation
    {
    public static void main (String [] args)
    {
    System.out.println ("This program will determine whether a year is a leap year!");

    Scanner x = new Scanner (System.in);
    System.out.println ("\nPlease enter a year: ");
    int year = x.nextInt ();

    if (year > 1582 && year / 4 == 0)
    {
    if (year / 100 != 0 && year / 400 == 0)
    {
    System.out.println ("\n" + year + " is a leap year.");
    }
    else
    {
    System.out.println ("\n" + year + " is not a leap year.");
    }
    }
    else if (year < 1582)
    {
    System.out.println ("\n" + year + " is not a valid number as the Gregorian calendar was not adopted yet.");
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot figure out why my code won't work!

    Can you explain what the problem is?
    Post copies of the console from when the code executes that shows its output and add some comments describing what is wrong and show what the output should be.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Spanky_10 (February 20th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Cannot figure out why my code won't work!

    Output:
    This program will determine whether a year is a leap year!

    Please enter a year:
    2016


    I enter a year and it does not give me any of the system.out.println statements like it should.

    import java.util.Scanner;
    public class StudentInformation
    {
        public static void main (String [] args)
        {
            System.out.println ("This program will determine whether a year is a leap year!");
     
            Scanner x = new Scanner (System.in);
            System.out.println ("\nPlease enter a year: ");
            int year = x.nextInt ();
     
            if (year > 1582 && year / 4 == 0)
            {
                if (year / 100 != 0 && year / 400 == 0)
                {
                    System.out.println ("\n" + year + " is a leap year.");
                }
                else
                {
                    System.out.println ("\n" + year + " is not a leap year.");
                }
            }
            else if (year < 1582)
            {
                System.out.println ("\n" + year + " is not a valid number as the Gregorian calendar was not adopted yet.");
            }
        }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot figure out why my code won't work!

    Please edit the code and properly format it to give indentations for nested statements.
    Statements should not all start in the first column.

    it does not give me any of the system.out.println statements
    To be sure that something prints out, add an else statement to the end of the if/else if chain that prints out a message saying what happened and why it is being executed.


    None of the if tests are true if nothing is printed.

    Try debugging the code by adding some println statements before the if statements that print out the results of all the expressions that the code uses in the if statements. You need to see what the computer sees when it executes the code and evaluates the expressions in the if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Cannot figure out why my code won't work!

    I fixed my code to proper format. I input a year, such as 2012, which should output: 2012 is a leap year. But instead nothing outputs. I get the output from my first S.o.p statement at the beginning explaining the program. 2012 satisfies the first if condition so I'm not sure how to fix it

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot figure out why my code won't work!

    nothing outputs
    Did you do this: add an else statement to the end of the if/else if chain that prints out a message saying what happened and why it is being executed.

    Post your new code with the println statements I suggested you add
    and post the output from that program that includes the printouts from the println statements that you added.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Cannot figure out why my code won't work!

    Quote Originally Posted by Spanky_10 View Post
    I fixed my code to proper format. I input a year, such as 2012, which should output: 2012 is a leap year. But instead nothing outputs. I get the output from my first S.o.p statement at the beginning explaining the program. 2012 satisfies the first if condition so I'm not sure how to fix it
    Are you sure?

Similar Threads

  1. Please help me figure out how to work with a linked list in a JFrame. Agh!
    By BloomingNutria in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 12th, 2012, 12:53 PM
  2. My polymorphism code won't work
    By Floomer99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2012, 05:31 AM
  3. Studying for a test, can't figure out my code won't work
    By coolidge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2011, 01:12 PM
  4. need to figure how this thing will work
    By frosst in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 26th, 2011, 01:45 AM
  5. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM

Tags for this Thread