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

Thread: What´s wrong?

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What´s wrong?

    First of all i´m a begginer and i know this code is very basic, but i really want to know the problem and how to fix it. There ar no compile error or logical errors i think, but nothing appears on the console. Can someone help? Thanks.

    import java.util.Scanner;

    public class A {
    public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the month: ");
    String month = input.nextLine();
    System.out.print("Enter the year: ");
    int year = input.nextInt();

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ // if it is a leap year
    if (month == "January")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "February")
    System.out.print(month + " " + year + " has " + "29 days");
    else if (month == "March")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "April")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "May")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "June")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "July")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "August")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "September")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "October")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "November")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "December")
    System.out.print(month + " " + year + " has " + "31 days");
    }
    else { // If it´s not a leap year
    if (month == "January")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "February")
    System.out.print(month + " " + year + " has " + "28 days");
    else if (month == "March")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "April")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "May")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "June")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "July")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "August")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "September")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "October")
    System.out.print(month + " " + year + " has " + "31 days");
    else if (month == "November")
    System.out.print(month + " " + year + " has " + "30 days");
    else if (month == "December")
    System.out.print(month + " " + year + " has " + "31 days");
    }
    }
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: What´s wrong?

    When comparing strings you NEED to use the "equals" method instead of "==".
    The == operator can only be used for primitive data (int, float, char, etc)

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What´s wrong?

    I tried to change to equals as you said but then this error occurs:" Type mismatch: cannot convert from String to boolean"

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What´s wrong?

    Please post your code using code or highlight tags as explained near the top of this topic.

    Don't tell us what you tried to do, SHOW us (post) what you tried to do with the resulting errors AND post your code correctly.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Clef (August 13th, 2014)

Similar Threads

  1. Replies: 4
    Last Post: March 6th, 2014, 05:14 PM
  2. Re: What wrong?
    By rajesh_rasu26 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 21st, 2012, 11:03 AM
  3. Not sure what is wrong
    By dremn2004 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 18th, 2011, 10:49 AM
  4. WHAT IS WRONG HERE PLEASE?
    By mjava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 27th, 2010, 08:29 PM
  5. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM