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

Thread: Java program inputting month name and outputting month number and number of days

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program inputting month name and outputting month number and number of days

    I need to write a program in java that asks the user to enter the name of a month. Then, using this string and switch statements, I need to output the integer value of that month. For example, if September is entered, it will output 9. There can only be one println() statement for the output string however.

    I then need to expand on that program to also tell the user the number of days in the month they've entered. I am supposed to use if-else/else-if statements to do this.

    I know this isn't right but this is what I have so far...

    import java.util.Scanner;
    public class Month {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter a month: ");
    		monthName = input.nextInt();
    		switch (monthName) {
    		case "January": month = 1; break;
    		case "February": month = 2; break;
    		case "March": month = 3; break;
    		case "April": month = 4; break;
    		case "May": month = 5; break;
    		case "June": month = 6; break;
    		case "July": month = 7; break;
    		case "August": month = 8; break;
    		case "September": month = 9; break;
    		case "October": month = 10; break;
    		case "November": month = 11; break;
    		case "December": month = 12; break;
     
    		}
    	System.out.println(month);
    		}
     
    	}

    I was just hoping somebody could point me in the right direction from here...
    Last edited by Brovahkiin501; September 18th, 2012 at 08:49 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    Do you have a question?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    is monthName a string or an int? you never declared it as anything

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    The problem is that monthName is an int, as you inputed. Then, with the switch/case method, you enter it as a string. It has to be one or the other, but not both. Personally, what I did was that I just made monthName an integer and did case(1,2,3,etc). Then, what you could do is enter the days in that case, such as when monthName is 1, days=31. It makes your life easier.

    OR what you can do is that you could enter month as an integer, and switch month. What you then do is name the monthName inside the case. As in, in case(1): monthName="January";days=31;

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    Quote Originally Posted by asundar View Post
    The problem is that monthName is an int, as you inputed. Then, with the switch/case method, you enter it as a string.
    That is correct.


    Quote Originally Posted by asundar View Post
    It has to be one or the other, but not both. Personally, what I did was that I just made monthName an integer and did case(1,2,3,etc). Then, what you could do is enter the days in that case, such as when monthName is 1, days=31. It makes your life easier.

    OR what you can do is that you could enter month as an integer, and switch month. What you then do is name the monthName inside the case. As in, in case(1): monthName="January";days=31;
    His instructions say otherwise.

    You are expected to switch over a String, your variable monthName must get, and hold, a String. You should also consider what will happen if I type august instead of August, and again if i type auguts. You can search the keyword default and possibly see more uses of switch case
    Last edited by jps; September 18th, 2012 at 09:02 PM. Reason: fixed paragraph

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    Does your instructor specify that the month integer value has to be a variable, and can't just be entered manually by you? IE:
    ...
    Last edited by copeg; September 19th, 2012 at 08:42 AM. Reason: Removed spoonfed code

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java program inputting month name and outputting month number and number of days

    ChicoTheMan94, please read the forum rules and the following: http://www.javaprogrammingforums.com...n-feeding.html

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Java program inputting month name and outputting month number and number of days

    Quote Originally Posted by copeg View Post
    ChicoTheMan94, please read the forum rules and the following: http://www.javaprogrammingforums.com...n-feeding.html
    Sorry, I had to do a similar assignment, so just copy/pasted what I had because I wrote in month numbers manually, it wasn't an answer to his assignment, though, since he has to use if/else statements to show the number of days in the month, where as I just predefined them as variables, which would most certainly give him a failing grade.

Similar Threads

  1. How much per a month?
    By benglish in forum Totally Off Topic
    Replies: 14
    Last Post: September 23rd, 2013, 04:52 PM
  2. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM
  3. Hire Our Senior Java Developer @ 2200 USD / month
    By citigo in forum Paid Java Projects
    Replies: 0
    Last Post: July 11th, 2011, 09:31 PM
  4. Looking for java developer for 2 month long engagement
    By amitkgupta28 in forum Paid Java Projects
    Replies: 0
    Last Post: April 19th, 2011, 03:23 AM
  5. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 11:13 AM