-3

Let's say if user needs to put some input in integer and the output will be in a month.

The first line of input is the number of members in a group. So for each of the following line there are 3 integers that represent a valid date which is the birthdate of each member in the format of dd mm yyyy. If a user insert number in for an example 1 01 2018 so output will become 1 January 2018.

This is my code.

import java.util.*;
import java.text.SimpleDateFormat;

public class LabExercise1b {

    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);
        System.out.print(" "); //number of people
        int people = console.nextInt();
        int day, year;
        String[] month = new String[]{"January", "February", "March", "April", "May", "June", "July", "August",
                "September", "October", "November", "December"};

        for (int i = 1; i <= people; i++) {
            SimpleDateFormat newFormat = new SimpleDateFormat("MM");
            System.out.println(" ");
            day = console.nextInt();
            year = console.nextInt();
        }
        System.exit(0);
    }
}

Btw, i am still new on learning java language. Hopefully someone can teach me or share some knowledge. I am using eclipse IDE.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Amirul Amin
  • 13
  • 1
  • 6
  • 2
    Explain clearly what is your necessity – NiVeR Sep 28 '18 at 10:59
  • 2
    The Month enum includes constants for the twelve months, why you have to use something else? https://docs.oracle.com/javase/tutorial/datetime/iso/enum.html – vmrvictor Sep 28 '18 at 11:00
  • you mean the day in a year? giving for example 25 is January and giving 36 is February? – vmrvictor Sep 28 '18 at 11:05
  • 1
    You can check this question https://stackoverflow.com/questions/1038570/how-can-i-convert-an-integer-to-localized-month-name-in-java – flyingfox Sep 28 '18 at 11:06
  • The first line of input is the number of members in a group so for each of the following line there are 3 integers that represent a valid date which is the birthdate of each member in the format of dd mm yyyy. If a user insert number in for an example 1 01 2018 so output will become 1 January 2018. – Amirul Amin Sep 28 '18 at 11:14
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). I certainly hope it’s not your teacher teaching you to use `SimpleDateFormat`. That would be bad teaching. – Ole V.V. Sep 29 '18 at 08:32
  • Does your code work as expected? Did you have a question? If your code misbehaves, please explain precisely how so we may help you. – Ole V.V. Sep 29 '18 at 08:34
  • Supplementary information for your question is very welcome, and specifying how your program should do is crucial to the question, so thank you for doing that. It’s always best to edit the question and add the information there so we have everything in one place. This time I did it for you. – Ole V.V. Sep 29 '18 at 08:39

3 Answers3

3

If your input is 11042018. And you want your output to be 11 April 2018, then Do something like this:

Scanner console = new Scanner(System.in);

String[] month = new String[]{"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"};

int input = console.nextInt();

String dateString = input.toString();
String day = dateString.subString(0, 1);
String monthNum = dateString.subString(1, 3);
int intMonthNum = Integer.toString(monthNum);
String year = dateString.subString(3, 7);

System.out.print(day + " ");

if(intMonthNum>0 && intMonthNum <13){
    System.out.print(month[intMonthNum-1]); 
}else{
    System.out.println("Enter correct month number"); 
}

System.out.print(" " + year);
  • 2
    month[input+1] --> will throw ArrayIndexOutOfBoundsException, if user enters 12. It should be month[input-1] – JavaLearner1 Sep 28 '18 at 11:15
  • Thanks. I mis-typed '+' sign. Anyway edited the answer. :) – Rupesh Singra Sep 28 '18 at 11:18
  • 1
    It said cannot cast from string to int for this line - if((int)monthNum>0 && (int)monthNum <13){ System.out.print(month[(int)monthNum-1]) and cannot invoke to String() on the primitive type int for input.toString(); what should i do? – Amirul Amin Sep 28 '18 at 14:58
1

Month as an enum is present in Java time API also which can be directly used:

import java.time.Month;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(Month.of(num).name());

    }
}

If you are not allowed to use java-8 or above. You can use user-defined enum in that case with a function which returns Month given Month's value as integer. For example:

enum Month {
    JANUARY(1), 
    FEBRURAY(2),
    MARCH(3),
    APRIL(4),
    MAY(5),
    JUNE(6),
    JULY(7),
    AUGUST(8),
    SEPTEMBER(9),
    OCTOBER(10),
    NOVEMBER(11),
    DECEMBER(12);

    private int value;

    private Month(int value) {
        this.value = value;
    }

    public static Optional<Month> valueOf(int value) {
        return Arrays.stream(values())
                .filter(month -> month.value == value)
                .findFirst();
    }

}

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(Month.valueOf(num).get());
    }
}
Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
1

I understand from your code in the question that you are allowed to use a built-in date formatter. That’s also a good idea. But: (1) Do use the modern DateTimeFormatter (not the outdated and troublesome SimpleDateFormat). (2) Now you’re at it, use it for formatting the entire date (not just the month).

    DateTimeFormatter outputFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.UK);

    Scanner console = new Scanner(System.in);
    int day = console.nextInt();
    int monthNumber = console.nextInt();
    int year = console.nextInt();
    LocalDate date = LocalDate.of(year, monthNumber, day);
    System.out.println(date.format(outputFormatter));

Example run:

1 01 2018
01 January 2018

Very often one would also use a DateTimeFormatter for parsing the user input in one go rather than parsing each number separately. Use your search engine to find examples.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161