Question

Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....

Create a Java Program/Class named MonthNames that will display the Month names using an array.

1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc.

2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year.

Once you have the value, the program needs to adjust the given input to align with the array index and then confirm that the variable is in the range of 0-11. If it is not, display "Invalid Entry". If it is in range, display the string the matches the given entry. Then prompt to continue.

Tip: For the String input for the Prompt to Continue - it is preferable to use .next() instead of .nextLine(). Otherwise you will run into the Scanner Bug described in Section 3.10.

3. Display "Done" after the loop processing has ended.

Enter the Month of the Year: 1 The month name is January

Try again (Y/N): Y

Enter the Month of the Year : 2 The Month name is February.

Try again (Y/N): Y

Enter the Month of the Year : 55 Invalid Entry

Try again (Y/N): N

You can copy the code below to seed your array.:

{"January","February","March","April","May","June","July","August","September","October","November","December"};

Homework Answers

Answer #1
import java.util.*;

public class Main {
        public static void main(String[] args) {
                
        String[] months={"January","February","March","April","May","June","July","August","September","October","November","December"};
        
        Scanner myKeyboard =new Scanner(System.in);
        String choice="";
        
        while(true){
            System.out.print("Enter the month of the year: ");
            int n=myKeyboard .nextInt();
            
            if(n>12 || n<1)
            System.out.println("Invalid Entry");
            else
            System.out.println("The month name is "+months[n-1]);
            
            System.out.println("Try Again(Y/N)");
            choice=myKeyboard .next();
            
            if(choice.compareTo("N")==0)
            break;
        }
        
     System.out.println("Done");
                
                
                
        }
}

Feel free to ask.

Kindly Upvote. ?

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Phone number lookup Design a program that has two parallel arrays: a string array named people...
Phone number lookup Design a program that has two parallel arrays: a string array named people that is initialized with the names of seven of your friends, and a string array named phoneNumbers that is initialized with your friends phone numbers. The program should allow the user to enter a persons name (or part of a persons name). it should then search for that person in the people array. If the person is found, it should get that persons phjone...
Create a program that allows the user to input a list of first names into one...
Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of emial addresses where the address is of the following form: [email protected] Declare FirstName[100] as String Declare LastName[100] as String Declare email as String Declare K as Integer Declare index as Integer Write "Enter first and last name." Write...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For Loop that asks for a number between 1 and 10; Will double the number each time through the loop and display the answer. Will run five times, so the number will have been doubled five times, and the answer displayed five times. After the loop, display a message saying 'It's done!'. 2. Uses a While Loop; Ask the user to input a friend's name....
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
How would I solve these python problems? A) File Display Download the file from here named...
How would I solve these python problems? A) File Display Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file. numbers.txt contains 22 14 -99 AB B) Error Check Float Input When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except...
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will...
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3 choice:0-999". Create a...
Create a new file named quiz_loops.py. b. Write your name and date in a comment. c....
Create a new file named quiz_loops.py. b. Write your name and date in a comment. c. Write a for loop which will display this set of values 2,4,6,8,10,12,14,16. d. Write a while which will display this set of values 16,13,10,7,4,1,-2. e. Write a for loop which will print ‘Loops are fun’ three times. f. Write a while loop that will prompt you for four numbers. Convert the number to an integer. Compute the power as power = power ** number....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT