# Thread: If statements, input, and strings

1. Member
Join Date
Oct 2009
Posts
9
Rep Power
0

## 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. 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. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
thanks so much

4. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
And if you were to compare objects with an equal sign use ==.

= sets the value. == compares things.

5. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
100 is divisible by 4. 100/4 = 25

7. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
First of all use code tags when pasting code. [ CODE] and [/ CODE] without the spaces in there.

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);

String month = kb.next();

int year = kb.nextInt```
Now try that and respond with your results.

9. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
The directions for the program say it should only read one line of input :\ so i cant do that

10. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
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();

System.out.println(month);
System.out.println(year);

11. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
wow...thank you so much, all that confusion over 1 little bracket haha

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•