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

Thread: Issues with IF else Statement

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    Bangalore!!
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Issues with IF else Statement

    Hi All,

    I'm not sure whats wrong with the following code. I skips to final else and prints the message, but doesn't validate the if conditions.
    import java.util.Scanner;
    public class Ifprograms {
       public static void main(String[] args)
       {
        String Mon;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Month you wish to determine the season : ");
            Mon = sc.next();
          //  System.out.println("Mon is " +Mon);
           String season;
        if(Mon == "December"||Mon == "January"||Mon == "February")
        season = "Winter";
           else if(Mon == "March" || Mon == "April" || Mon == "May")
        season = "Spring";
           else if(Mon == "July" || Mon == "August" || Mon == "June")
        season = "Summer";
           else if(Mon == "September" || Mon == "October" || Mon == "November")
        season = "Autumn";
            else
            season = "Invalid Month";
     
        System.out.println(Mon+" is in the " +season+ ".");
       }
       }
    Last edited by helloworld922; October 31st, 2012 at 12:21 PM. Reason: please use [code] tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,324
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: Issues with IF else Statement

    Don't use == with Strings, or any Objects, unless you want to test whether they are the same instance. Use the equals() method instead. Do a search on google or these forums for a better explanation of why.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Location
    Bangalore!!
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with IF else Statement

    Thank You Kevin!!!

    That worked!! below is the code!
    import java.util.Scanner;
    public class Ifprograms {
       public static void main(String[] args)
       {
        String Mon;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Month you wish to determine the season : ");
            Mon = sc.next();
            System.out.println("Mon is " +Mon);
           String season;
        if (Mon.equals("December")||Mon.equals("January")||Mon.equals("February"))
        season = "Winter";
           else if(Mon.equals("March")||Mon.equals("April")||Mon.equals("May"))
        season = "Spring";
           else if(Mon.equals("June")||Mon.equals("July")||Mon.equals("August"))
        season = "Summer";
           else if(Mon.equals("September")||Mon.equals("October")||Mon.equals("November"))
        season = "Autumn";
        //System.out.println(Mon+"is in the " +season+ ".")
        else
            season = "Invalid Month";
     
        System.out.println(Mon+" is in the " +season+ ".");
       }
       }
    Last edited by jps; November 2nd, 2012 at 01:25 PM. Reason: added code tags

Similar Threads

  1. Jlabel issues
    By Wadashe in forum Java Theory & Questions
    Replies: 2
    Last Post: February 27th, 2012, 01:39 PM
  2. [SOLVED] Having Issues Wish this Program
    By userct in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 9th, 2012, 02:46 PM
  3. Noobie issues?
    By sukuiichuu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 05:10 AM
  4. issues with Class
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 20th, 2010, 12:49 PM
  5. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 07:49 AM

Tags for this Thread