Results 1 to 17 of 17
- 10-22-2009, 07:40 PM #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
- 10-22-2009, 07:45 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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 #3
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
thanks so much
- 10-22-2009, 07:57 PM #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.
- 10-22-2009, 08:43 PM #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
- 10-22-2009, 09:02 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
100 is divisible by 4. 100/4 = 25
- 10-22-2009, 09:11 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
thats what i have now but for some reason the year wont printJava 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."); } } } }Last edited by evolvepwnz; 10-22-2009 at 09:22 PM.
- 10-22-2009, 09:18 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.
Secondly, instead of this
use two different questions to get the answer from the user like thisJava Code:System.out.println("Please type a month and year: "); Scanner kb = new Scanner(System.in); String month = kb.next(); int year = kb.nextInt();
Now try that and respond with your results.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
- 10-22-2009, 09:23 PM #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-22-2009, 09:26 PM #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();
try adding the lines
System.out.println(month);
System.out.println(year);
- 10-22-2009, 09:30 PM #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
after my if statement of the month it doesnt printJava Code:System.out.println(month); System.out.println(year);
- 10-22-2009, 09:36 PM #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");
- 10-22-2009, 09:38 PM #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.)
- 10-22-2009, 09:42 PM #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.
- 10-22-2009, 09:48 PM #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
- 10-22-2009, 09:53 PM #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."); }
- 10-22-2009, 09:58 PM #17
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 09:50 PM -
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 09:26 AM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks