Results 1 to 17 of 17
  1. #1
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default If statements, input, and strings

    I'm trying to write a program that has a user input a month.. ex. October
    and have it output 10..but im having some trouble..it wont let me do

    if (month = october)
    system.out.println("10")
    else if (month = november)
    system.out.println("11")

    you guys get the idea...but i cant do month = october as an if statement...Im not sure how i would go about doing this and i understand this is a very simple question but im new and just cant figure it out

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

    Default

    For now do month.equals("October") and use 12 if statements; if you have that working read up on the Map interface and its HashMap implementation (or any implementation for that matter).

    kind regards,

    Jos

  3. #3
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    thanks so much

  4. #4
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    And if you were to compare objects with an equal sign use ==.

    = sets the value. == compares things.

  5. #5
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    alright so now i gotta be able to tell if a year is a leap year...to do that a year is a leap year if evenly divisible by 4, except in the case where the year is divisible by 100 but if the year is divisble by 400 then its a leap year..for example 2009 and 2010 are not leap years but 2008 is.........im so lost how the hell am i supposed to do that lol

  6. #6
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    100 is divisible by 4. 100/4 = 25

  7. #7
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
     System.out.println("Please type a month and year: ");
            Scanner kb = new Scanner(System.in);
            String month = kb.next();
            int year = kb.nextInt();
    
    
            if (month.equalsIgnoreCase("january")) {
                System.out.println("Month: 1");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("february")) {
                System.out.println("Month: 2");
                System.out.println("Days: 28");
            } else if (month.equalsIgnoreCase("march")) {
                System.out.println("Month: 3");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("april")) {
                System.out.println("Month: 4");
                System.out.println("Days: 30");
            } else if (month.equalsIgnoreCase("may")) {
                System.out.println("Month: 5");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("june")) {
                System.out.println("Month: 6");
                System.out.println("Days: 30");
            } else if (month.equalsIgnoreCase("july")) {
                System.out.println("Month: 7");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("august")) {
                System.out.println("Month: 8");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("september")) {
                System.out.println("Month: 9");
                System.out.println("Days: 30");
            } else if (month.equalsIgnoreCase("october")) {
                System.out.println("Month: 10");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("november")) {
                System.out.println("Month: 11");
                System.out.println("Days: 30");
            } else if (month.equalsIgnoreCase("december")) {
                System.out.println("Month: 12");
                System.out.println("Days: 31");
            } else {
                System.out.println("Not a month!");
    
                if (year % 4 == 0) {
                    if (year % 100 != 0) {
                        System.out.println(year + " is a leap year.");
                    } else if (year % 400 == 0) {
                        System.out.println(year + " is a leap year.");
                    } else {
                        System.out.println(year + " is not a leap year.");
                    }
                } else {
                    System.out.println(year + " is not a leap year.");
                }
            }
        }
    }
    thats what i have now but for some reason the year wont print
    Last edited by evolvepwnz; 10-22-2009 at 09:22 PM.

  8. #8
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    First of all use code tags when pasting code. [ CODE] and [/ CODE] without the spaces in there.

    Secondly, instead of this

    Java Code:
    System.out.println("Please type a month and year: ");
    Scanner kb = new Scanner(System.in);
    String month = kb.next();
    int year = kb.nextInt();
    use two different questions to get the answer from the user like this
    Java Code:
    Scanner kb = new Scanner(System.in);
    
    System.out.println("Please type a month");
    String month = kb.next();
    
    System.out.println(Please type a year");
    int year = kb.nextInt
    Now try that and respond with your results.

  9. #9
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    The directions for the program say it should only read one line of input :\ so i cant do that

  10. #10
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Then as a test to see what values are actually in your variables, right after these lines

    System.out.println("Please type a month and year: ");
    Scanner kb = new Scanner(System.in);
    String month = kb.next();
    int year = kb.nextInt();

    try adding the lines
    System.out.println(month);
    System.out.println(year);

  11. #11
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    yea its showing the month and the year i type..but if i put the
    Java Code:
    System.out.println(month);
    System.out.println(year);
    after my if statement of the month it doesnt print

  12. #12
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Well if it prints the correct values right away but doesn't print in your IF statement, that means it's not getting in your IF statement. Try putting random println's inside of your IF statements just to see if it's getting there, and try running your program with the same criteria everytime.

    Like this
    Java Code:
    System.out.println("Please type a month and year: ");
            Scanner kb = new Scanner(System.in);
            String month = kb.next();
            int year = kb.nextInt();
    
    
            if (month.equalsIgnoreCase("january")) {
                [COLOR="DarkRed"][COLOR="Red"]System.out.println("Is my program getting inside january?");[/COLOR][/COLOR]
                System.out.println("Month: 1");
                System.out.println("Days: 31");
            } else if (month.equalsIgnoreCase("february")) {
                [COLOR="red"]System.out.println("Is my program getting inside february?");[/COLOR]
                System.out.println("Month: 2");
                System.out.println("Days: 28");

  13. #13
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    heres the directions for the program..if your bored enough maybe you could help lol

    Write a program that will read one line from the input which contains the name of the month and a year. For example it might look like

    October 2009

    The output of the program will be three lines: first a statement of the number of the month (10 for october). Second, the number of days in the month (31 for october) and finally an indication of whether the year is a leap year or not. (A year is a leap year if it is evenly divisible by 4, except in the case where the year is divisible by 100, but if the year is divisible by 400 then it is a leap year. For example, 2009 and 2010 are not leap years while 2008 is. The year 1900 was not a leap year, but 2000 was.)

  14. #14
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    You seem like you're on the right track but it's not getting inside of your loop for some reason. Try the test I said above in post #12 and enter january for your month.

  15. #15
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    ok so i sorta did what you said and realized this...it only only go to the Leap Year if statement if i type in like "bob 2012" if i type an actual month like "october 2012" it wont continue to the leap year if statement that i have...Im not sure how to fix that but im almost positive its something with the else and else if

  16. #16
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Yes that is because in your else statement for not a month, you have an IF staement inside of their a.k.a. a nested if statement

    This is the end of your code. The red part only executes if it's not a month. If you wanted to check for a month, and then check for a leap year, you'd have to end your else satement with a closing bracket and then do the leap year IF staements.
    Java Code:
    } else {
                System.out.println("Not a month!");
    
                [COLOR="Red"]if (year % 4 == 0) {
                    if (year % 100 != 0) {
                        System.out.println(year + " is a leap year.");
                    } else if (year % 400 == 0) {
                        System.out.println(year + " is a leap year.");
                    } else {
                        System.out.println(year + " is not a leap year.");
                    }
                } else {
                    System.out.println(year + " is not a leap year.");
                }[/COLOR]
            }
    Java Code:
    } else {
                System.out.println("Not a month!");
    [COLOR="red"]}   <-- this bracket is the key[/COLOR]  
    
    
                if (year % 4 == 0) {
                    if (year % 100 != 0) {
                        System.out.println(year + " is a leap year.");
                    } else if (year % 400 == 0) {
                        System.out.println(year + " is a leap year.");
                    } else {
                        System.out.println(year + " is not a leap year.");
                    }
                } else {
                    System.out.println(year + " is not a leap year.");
                }

  17. #17
    evolvepwnz is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    wow...thank you so much, all that confusion over 1 little bracket haha

Similar Threads

  1. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  2. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  3. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  4. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 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
  •