I have been given an assignment by my professor to create a Monthnum class with multiple constuctors to take all argument, as we are just learning Object Oriented Programming. I needed to create a new constructor that accepts user input both as an int value and another constructor that accepts it as a string value for the month of the year. Ex: 1 = January and January = 1. I know that I can create a scanner in my main method, but I'm unsure of how to get this number to be accepted and printed out. A step in the right direct would be extremely useful!
import java.util.Scanner;
public class learningObjectsAndClasses {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int monthNumber = input.nextInt();
String monthName = input.nextLine();
Monthnum inputMonthNumber = new Monthnum(monthNumber);
Monthnum inputMonthName = new Monthnum(monthName);
System.out.println("Please enter the month name or number: "
+ inputMonthNumber);
}
}
class Monthnum{
int Monthnum;
String monthName;
Monthnum(){
Monthnum = 1;
}
Monthnum(int whichMonth){
Monthnum = whichMonth;
if (whichMonth == 1){
System.out.println("January");
}
else if (whichMonth == 2){
System.out.println("February");
}
else if (whichMonth == 3){
System.out.println("March");
}
else if (whichMonth == 4){
System.out.println("April");
}
else if (whichMonth == 5){
System.out.println("May");
}
else if (whichMonth == 6){
System.out.println("June");
}
else if (whichMonth == 7){
System.out.println("July");
}
else if (whichMonth == 8){
System.out.println("August");
}
else if (whichMonth == 9){
System.out.println("September");
}
else if (whichMonth == 10){
System.out.println("October");
}
else if (whichMonth == 11){
System.out.println("November");
}
else if (whichMonth == 12){
System.out.println("December");
}
else
System.out.println("Invalid input");
}
Monthnum(String whichMonth){
if (whichMonth == "January"){
Monthnum = 1;
}
else if (whichMonth == "February"){
Monthnum = 2;
}
else if (whichMonth == "March"){
Monthnum = 3;
}
else if (whichMonth == "April"){
Monthnum = 3;
}
else if (whichMonth == "May"){
Monthnum = 4;
}
else if (whichMonth == "June"){
Monthnum = 5;
}
else if (whichMonth == "July"){
Monthnum = 6;
}
else if (whichMonth == "August"){
Monthnum = 7;
}
else if (whichMonth == "September"){
Monthnum = 8;
}
else if (whichMonth == "October"){
Monthnum = 9;
}
else if (whichMonth == "November"){
Monthnum = 10;
}
else if (whichMonth == "December"){
Monthnum = 11;
}
else if (whichMonth == "March"){
Monthnum = 12;
}
else
System.out.println("Invalid input");
}
}