# If statements, input, and strings

• 10-22-2009, 07:40 PM
evolvepwnz
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
• 10-22-2009, 07:45 PM
JosAH
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
• 10-22-2009, 07:51 PM
evolvepwnz
thanks so much
• 10-22-2009, 07:57 PM
toymachiner62
And if you were to compare objects with an equal sign use ==.

= sets the value. == compares things.
• 10-22-2009, 08:43 PM
evolvepwnz
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
• 10-22-2009, 09:02 PM
toymachiner62
100 is divisible by 4. 100/4 = 25
• 10-22-2009, 09:11 PM
evolvepwnz
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
• 10-22-2009, 09:18 PM
toymachiner62
First of all use code tags when pasting code. [ CODE] and [/ CODE] without the spaces in there.

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
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.
• 10-22-2009, 09:23 PM
evolvepwnz
The directions for the program say it should only read one line of input :\ so i cant do that
• 10-22-2009, 09:26 PM
toymachiner62
Then as a test to see what values are actually in your variables, right after these lines

Quote:

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

Quote:

System.out.println(month);
System.out.println(year);
• 10-22-2009, 09:30 PM
evolvepwnz
yea its showing the month and the year i type..but if i put the
Code:

```System.out.println(month); System.out.println(year);```
after my if statement of the month it doesnt print
• 10-22-2009, 09:36 PM
toymachiner62
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
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");```
• 10-22-2009, 09:38 PM
evolvepwnz
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.)
• 10-22-2009, 09:42 PM
toymachiner62
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.
• 10-22-2009, 09:48 PM
evolvepwnz
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
• 10-22-2009, 09:53 PM
toymachiner62
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.
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]         }```
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.");             }```
• 10-22-2009, 09:58 PM
evolvepwnz
wow...thank you so much, all that confusion over 1 little bracket haha