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

Thread: Months of the year []

  1. #1
    Junior Member
    Join Date
    May 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Months of the year []

    I have been assigned a tutorial for my java class which requires me to write a simple program(compiled and launched via CMD) which asks the user to input a number and based on the users input, the appropriate month of the year should be displayed. The code I have tried looks something like this:

    import java.util.Scanner;
     
    /* Calender.java
     * This program displays the month of the year based on the users input
     * Date created: 5 May 2018 */
     
    public class Calender 
    {
        public static void main(String args[])
        {
    	Scanner sc = new Scanner(System.in);
    	String months[] = {"January", "February", "March", "April", "May", "June", "July, "August", "September", "October", "November", "December"};
    	int monthYear[] = {1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12};
    	int i;
     
    	System.out.print("Please enter a value to view the corrosponding month");
    	i = sc.nextInt();
     
    	for(i = 0; i <= 11; i++)
    	{
    		System.out.printf("Number %d: %s \n",months[i],monthYear[i]);
    	}
        }
    }
    Last edited by #0Prog; May 5th, 2018 at 11:04 AM. Reason: code tags added

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Months of the year []

    Why use a loop? You are attempting to do a single thing (display the month) not trying to do something 12 times. Try a single System.out.printf() statement to print the thing you want.

    (By the way, the most idiomatic way to write the loop would be for(i=0; i<12; i++) because that makes it clearer to the eye that the loop is executing 12 times.)

  3. #3
    Junior Member
    Join Date
    May 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Months of the year []

    Hi thanks for the advice, I am new to this and still learning a lot. the code now looks like this

    public class Calender
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    String[] months = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int[] monthYear = {0, 1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12};
    int i;

    System.out.print("Please enter a value to view the corrosponding month: ");
    i = sc.nextInt();


    System.out.printf(months[i],monthYear[i]);
    }
    }

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    170
    My Mood
    Asleep
    Thanks
    5
    Thanked 12 Times in 12 Posts

    Default Re: Months of the year []

    well done
    Whatever you are, be a good one

Similar Threads

  1. Can a database application be developed in 2 months?
    By Camaran in forum Java Theory & Questions
    Replies: 4
    Last Post: March 21st, 2014, 11:25 AM
  2. Small webapp that determine whether a year is a leap year.
    By frankhvam in forum Object Oriented Programming
    Replies: 0
    Last Post: October 26th, 2013, 03:32 PM
  3. numbered months
    By JavaN00b101 in forum Java Theory & Questions
    Replies: 5
    Last Post: February 8th, 2012, 12:13 PM