1

Ok, i'm very new to java and coding overall, and I have this assignment where I have to take user input , which will be a month, day and year, and multiply the month by the day and compare it to the last two digits of the year to check wether its a magic day or its not. While I accomplished this if the user only uses numbers as an input, I want to try and do another program where the users inputs the Month(Ex. April(not 4)) and I want to see if its possible for the program to check the enum, check for April and its value, and assign its value to the String month which then I will convert to a int. Sorry if my explanation is messy, But I explained to the best of my ability and feel free to ask if your confused about something.

Here's my code so far:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package magicadvanced;
import java.util.Scanner;
/**
 *
 * @author yfernandez
 */
public class MagicAdvanced {
    public enum Months{
        January(1),February(2),March(3),April(4),May(5),June(6),July(7),August(8),September(9),October(10),November(11),December(12);

        private int value;

        Months(int value){
            this.value = value;
        }


        public int valueInt(){
            return value;
        }
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner ls=new Scanner(System.in);
        String month,year,yrTwo;
        int day,yearInt;
        System.out.println("Please enter a month.");
        month = ls.next();
        System.out.println("Please enter a day.");
        day = ls.nextInt();
        System.out.println("Please enter the year.");
        year = ls.next();
        yrTwo = year.substring(2,4); //getting last two characters of the string year
        yearInt = Integer.valueOf(yrTwo);// converting last two character of the string year into an integer
        System.out.println(yearInt*2);//this is a just a test code to check if my conversion to integer works, will remove when program is done

    }
}

2 Answers2

0

My advice is to almost never try to create a custom enum for temporal units. The Java API already provides all the necessary tools to handle dates. Esp. Java 8, that brought significant improvements. You should take a look at resources like this and this for insights on the main features of the Java datetime API.

The following code snippet gives you an idea how to process months.

Month month = Month.valueOf("FEBRUARY");

System.out.println(month.getValue());
// output: 2
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.UK));
// output: February

In case you want to parse a String representing a date, you should use should use DateTimeFormatter, as for e.g. shown here.

Community
  • 1
  • 1
Iulian Rosca
  • 1,005
  • 4
  • 15
  • 30
0

Yes, you need to parse the string input and convert that into an Enum value of the month constants

Months x = Months.valueOf("JAnuary")

will init x to january...

then getting the value is the same you did:

int val = x.getValue()

then after that you can do all you need with the int.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97