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 5 of 5

Thread: Code is not giving the desired output

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code is not giving the desired output

    So im a begineer in java and im trying to write a code to tell me how many days there are in a specific month but febuary is giving 29 no matter if the year is a leap year or not and i cant seem to find the reason I would really apreciate some help as i have been staring and revising this for at least an hour to no avail.Thanks to anyone who helps.


    import java.util.Scanner;
    import static java.lang.System.out;
    import static java.lang.System.in;
    public class Calander {
     
    	public static void main(String[] args) {
     
    		Scanner sc = new Scanner(in);
    		int year;
    		int leapyear;
    		int month;
    		boolean leapyearTF = false;
     
    		out.print("Enter the year: ");
    			year = sc.nextInt();
     
    		out.print("Enter the month: ");	
    			month = sc.nextInt();
     
    		leapyear = year % 4;
     
    		if(leapyear == 0){
    			leapyearTF = true;
    			}else
    				leapyearTF = false;
    			leapyearTF = false;
     
    		switch(month){
     
    		case 1:
    		case 3:
    		case 5:
    		case 7:
    		case 8:
    		case 10:
    		case 12:	
     
    			out.println("There are 31 days in this month");
    				break;
     
    		case 4:
    		case 6:
    		case 9:
    		case 11:
     
    			out.println("There are 30 days in this month");
     
    		case 2:
    			if(leapyearTF = true){
     
    				out.print("There are 29 days in this month");
    			}else 
    				out.print("There are 28 days in this month");
     
    				break;
    	 }
     
      }
     
    }
    Last edited by Tree_Bek; May 10th, 2013 at 07:28 PM. Reason: Fixed


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is not giving the desired output

    Can you post the contents of the console window that shows what was entered into the program and what the program printed out when the program was executed?
    Add some comments to the output explaining what is wrong with it.

    if(leapyearTF = true){
    There is a problem here. = is the assignment operator and == is the equality testing operator.
    There is no need to compare a boolean variable to true or false. This will work:
    if(leapyearTF){
    The if() statement requires an expression that has a boolean value. A boolean variable has that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code is not giving the desired output

    Okay,

    Enter the year: 1998
    Enter the month: 2
    There are 29 days in this month


    I would like the output to read 28 days rather than 29 beacause 1998 is not a leap year. The reason its wrong is 1998 % 4 is 2 not 0 so leapyearTF should be false ,but it still prints 29 days.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is not giving the desired output

    See the second half of post#2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code is not giving the desired output

    Thank you!

Similar Threads

  1. For loop not giving the correct output
    By Kattracks32 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 28th, 2013, 04:41 AM
  2. My loops are working, but not giving right output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 14th, 2013, 07:55 PM
  3. Replies: 14
    Last Post: March 14th, 2012, 07:17 PM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. No Error in Code but Output is not desired...!!!
    By Adi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2011, 08:56 AM