1

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");
    }
}
3
  • The point of the exercise is to learn about constructors, yes? If so, are you sure that you should be including implementations of scanners within those constructors? Keep in mind that the purpose of a constructor is to help set up an object for use. You generally wouldn't be including functionality within your constructor beyond receiving arguments and assigning those arguments to fields local to the object. You should verify your understanding of the exercise with your instructor. Commented Feb 26, 2016 at 3:46
  • Have you had arrays yet? Surely there's a better way than the long chain of if-else that you have now. Commented Feb 26, 2016 at 3:46
  • I might change it to an array in the future, but for right now I just wanted to get the program working Commented Feb 26, 2016 at 3:59

1 Answer 1

1

So after you setup the scanner and everything. You want to read the value that a user would input. You would read that with Scanner's .nextInt() method.

Scanner input = new Scanner(System.in);
int num;   
num = input.nextInt();

Then after reading it and saving it in a variable num, You can instantiate an object of Monthnum with a given parameter that you got from user.

Monthnum m = new Monthnum(num);

And if you want to read the String from user, you can user Scanner's nextLine() method like this:

Scanner input = new Scanner(System.in); //Same scanner from above 
                                        //no need to initialize it again
String s;   
s = input.nextLine();

Now you would declare and initialize an object of Monthnum with String parameter (which you haven't made yet)

Monthnum p = new Monthnum(s);

Now here is a hint for making that constructor. You use those same if else that you already have. Just change them to like this:

if (whichMonth.equals("January"){
        System.out.println("a");
}
....

Do ask me questions if you don't understand something! thanks

EDIT: So now your code should look like this:

package tst;

import java.util.Scanner;

public class learningObjectsAndClasses {
public static void main(String args[]){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter Month Name ");
    String monthName = input.next();
    Monthnum inputMonthNumber = new Monthnum(monthName);

    System.out.println("Enter Number ");
    int monthNumber = input.nextInt();
    Monthnum inputMonthName = new Monthnum(monthNumber);

}
public static class Monthnum{

    public int Monthnum;
    public String monthName;


    public Monthnum(){
        Monthnum = 1;
        monthName = "January";
    }
    public Monthnum(int whichMonth){
        Monthnum = whichMonth;
        if (whichMonth == 1){
            monthName = "January";
            System.out.println("January");
        }
        else if (whichMonth == 2){
            monthName = "February";
            System.out.println("February");
        }
        else if (whichMonth == 3){
            monthName = "March";
            System.out.println("March");
        }
        else if (whichMonth == 4){
            monthName = "April";
            System.out.println("April");
        }
        else if (whichMonth == 5){
            monthName = "May";
            System.out.println("May");
        }
        else if (whichMonth == 6){
            monthName = "June";
            System.out.println("June");
        }
        else if (whichMonth == 7){
            monthName = "July";
            System.out.println("July");
        }
        else if (whichMonth == 8){
            monthName = "August";
            System.out.println("August");
        }
        else if (whichMonth == 9){
            monthName = "September";
            System.out.println("September");
        }
        else if (whichMonth == 10){
            monthName = "October";
            System.out.println("October");
        }
        else if (whichMonth == 11){
            monthName = "November";
            System.out.println("November");
        }
        else if (whichMonth == 12){
            monthName = "December";
            System.out.println("December");
        }
        else {
            System.out.println("Invalid input from int cons");
        }

    } //end of cons

    public Monthnum(String whichMonth){
        if (whichMonth.equals("January")){
            Monthnum = 1;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("February")){
            Monthnum = 2;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("March")){
            Monthnum = 3;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("April")){
            Monthnum = 4;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("May")){
            Monthnum = 5;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("June")){
            Monthnum = 6;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("July")){
            Monthnum = 7;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("August")){
            Monthnum = 8;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("September")){
            Monthnum = 9;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("October")){
            Monthnum = 10;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("November")){
            Monthnum = 11;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("December")){
            Monthnum = 12;
            System.out.println(Monthnum );
        }
        else
            System.out.println("Invalid input");
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! This was an enormous help, I knew I was right there, but just couldn't think of it.
simplest do you have Steam?
@simplest : When you type this: Monthnum inputMonthNumber = new Monthnum(monthNumber); that should automatically print the name of the month. If this doesn't solve your problem, maybe I didn't understand your question. Can you post the updated code on the main question post please? thanks
I updated my current code. I am having no output in eclipse.
@simplest I gave you the hint in my original answer but I don't know what you did. Anyway, I have posted the complete answer answer edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.