-2

I am trying to execute following code and found that for loop is not executing at all. Even not getting any error. Let me clear that "dateList" is a list of string and it's not empty, I already tried printing that. "monthList" is an ArrayList of string.

private ArrayList<String> monthList = new ArrayList<String>(Arrays.asList("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"));
private List<String> dateList = new ArrayList<>();
private List<Date> finalDatesInMMMDDYYYYFormate = new ArrayList<>();
private int year = 2019;



 for (int m=0; m<dateList.size(); m++){
        System.out.println("Date at position ------ "+m+" is -------"+dateList.get(m));
    } // length of this list is 9 & it prints eberything from 0 to 9. It is not empty.
    int assignYear = year;
    for (int k=1; k<dateList.size(); k++){
        if (dateList.get(k).substring(0,2)==dateList.get(k-1).substring(0,2) || dateList.get(k).substring(0,2) == monthList.get(0)){
            finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
        }
        else if (dateList.get(k).substring(0,2) == monthList.get(11)){
            finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear-1).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
            assignYear = assignYear-1;
        }
        else {
            for (int l=0; l<monthList.indexOf(dateList.get(k-1).substring(0,2)); l++){
                if (dateList.get(k).substring(0,2) == monthList.get(l)){
                    finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
                    break;
                }
                else {
                    finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear-1).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
                    assignYear--;
                    break;
                }
            }
        }
    }
Jigar Patel
  • 21
  • 1
  • 4

2 Answers2

1

You appear to never be adding anything to dateList, so it's empty when you start the loop. Thus there's nothing to loop over.

Jordan
  • 2,273
  • 9
  • 16
0

dateList has nothing added to it. Its size is therefore 0. As m is then equal to dateList.size() the for loop doesn't execute.

M. Goodman
  • 141
  • 9
  • I already added to dateList and it has size 9. checked that by printing all values. – Jigar Patel Feb 13 '19 at 19:27
  • In the code posted dateList is declared directly before the for loop. The dateList in this code block has nothing in it. If this is the way it is written in your program the size of the dateList when the for loop is evaluated is 0. – M. Goodman Feb 13 '19 at 19:29