Home » Java » java – Is there a way to dynamically create a counter if such "month" exists?-Exceptionshub

java – Is there a way to dynamically create a counter if such "month" exists?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I would like to enquire or get some reference as to how can I dynamically create a counter for each month if the exists ? Currently, I am retrieving the dates from a CSV file and store it in an ArrayList, from there I am comparing the dates to check whether if such month exists. If the month exists then “counter++”. Afterwards, store the counter in a hashmap. I understand my code currently is an inefficient way of coding. How could I make it better ?


public HashMap<String, Integer> getDataPoint() {
    //My function code
    HashMap<String, Integer> numberOfPost = new HashMap<String, Integer>();
    int janCounter = 0;
    int febCounter = 0;
    int marCounter = 0;
    int aprCounter = 0;
    int mayCounter = 0;
    int juneCounter = 0;
    int julyCounter = 0;
    int augCounter = 0;
    int septCounter = 0;
    int octCounter = 0;
    int novCounter = 0;
    int decCounter = 0; 
    String pattern = "MMM";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    OpenCsvReader reader = new OpenCsvReader();
    ArrayList <STPost> STArray = reader.loadST("file_path");

    Iterator STitr = STArray.iterator();
    while (STitr.hasNext()) {
        STPost St = (STPost) STitr.next();
        Date retrievedate = St.getTime();
        String strDate = sdf.format(retrievedate);
        if(strDate.equals("Jan")) {
        else if (strDate.equals("Feb")) {
        else if (strDate.equals("Mar")) {
        else if (strDate.equals("Apr")) {
        else if (strDate.equals("May")) {
        else if (strDate.equals("June")) {
        else if (strDate.equals("July")) {
        else if (strDate.equals("Aug")) {
        else if (strDate.equals("Sept")) {
        else if (strDate.equals("Oct")) {
        else if (strDate.equals("Nov")) {
        else if (strDate.equals("Dec")) {
        numberOfPost.put("January", janCounter);
        numberOfPost.put("Feburary", febCounter);
        numberOfPost.put("March", marCounter);
        numberOfPost.put("April", aprCounter);
        numberOfPost.put("May", mayCounter);
        numberOfPost.put("June", juneCounter);
        numberOfPost.put("July", julyCounter);
        numberOfPost.put("August", augCounter);
        numberOfPost.put("September", septCounter);
        numberOfPost.put("October", octCounter);
        numberOfPost.put("November", novCounter);
        numberOfPost.put("December", decCounter);
  return numberOfPost
How to&Answers:

You can create an array of months and check if value exists there using indexOf method.

String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
Integer idx = months.indexOf(strDate);

Thereafter you can use SimpleDateFormat("MMMM") pattern to put and get it into your map.

if(idx > -1) {
    String longDate = new SimpleDateFormat("MMMM").format(retrievedate);
    Integer current = numberOfPost.get(longDate);
    if (current == null) {
        current = 1;
    } else {
        current += 1;
    numberOfPost.put(longDate, current);

Thereafter, you can use map iterator to display content of map.


You already have a good start. Using a the hash map will make the code much tidier.
You can replace all those if statements and put statements with the code below:

if (!numberOfPosts.containsKey(strDate)) {
    numberOfPosts.put(strDate, 0);
numberOfPosts.put(strDate, numberOfPosts.get(strDate) + 1);

The if statement will create a dictionary entry if there is not one with the key of strDate. The value of the entry is set to 0.

numberOfPosts.put(strDate, numberOfPosts.get(strDate) + 1)

The line above increments by 1 the dictionary entry with the key of strDate.