-3

i have a list of strings which is from a loop statement and i don't know how to make it an ArrayList then call the strings individually. here is the while statement :

try {
    toDate=format.parse(str4);
    java.util.Date newValue= new SimpleDateFormat(OLD_FORMAT).parse(str4);
    String newValue2 = new SimpleDateFormat(NEW_FORMAT).format(newValue);
    // System.out.println(newValue2);
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}


//change the format of toDate 
try {
    newDateString=format.parse(str5);
    java.util.Date newqwe = new SimpleDateFormat(OLD_FORMAT).parse(str5);
    String newqwe2=new SimpleDateFormat (NEW_FORMAT).format(newqwe);
    //System.out.println(newqwe2);
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

//get the days between two dates then print it 
Calendar cal2 = Calendar.getInstance();
cal2.setTime(toDate);
while (cal2.getTime().before(newDateString)) {
    cal2.add(Calendar.DATE, 1);
    String datelist=(format.format(cal2.getTime()));

str4 is from a label which is a date same as str5, when a button is clicked the strings from the loop statement will be like this:

2013-05-03
2013-05-04
2013-05-05
2013-05-05
2013-05-06
2013-05-07
2013-05-08
2013-05-09
2013-05-10
2013-05-11
2013-05-12
2013-05-13
2013-05-14
2013-05-15
2013-05-16
2013-05-17
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Maguzu
  • 433
  • 3
  • 7
  • 14

1 Answers1

2
List<String> list = new ArrayList<String>();

 list.add("2013-05-03");
 list.add("2013-05-04");
 ........etc

 for(String str:list)
  System.out.prinltn(str); // prints all the strings in list


System.out.println(list.get(index)); // prints string at specified index
PSR
  • 39,804
  • 41
  • 111
  • 151
  • this will print the whole list, what if i want to print one string from the list of strings. if i will do this i can easily print datelist instead. – Maguzu May 31 '13 at 05:06
  • `System.out.println(list.get(index));` where *index* is the position of the String you would like to print. – Hunter McMillen May 31 '13 at 05:07
  • i cant add the strings individually because they are from a while loop, is there a way I can add then while running the loop? the strings from the while statement is not constant they change depending on the dates from str4 and str5. – Maguzu May 31 '13 at 05:16
  • you can use this code in while loop by declaring list out side loop – PSR May 31 '13 at 05:18
  • @JohnPaulMagat you should at least try using `List ls = new ArrayList();` and then come with something else. From your post, looks like you tried nothing and are in *gimme the codez* mode. – Luiggi Mendoza May 31 '13 at 05:21
  • @PSR: is this right? System.out.println(list.get(2)); which will print the third string from the list of strings right? – Maguzu May 31 '13 at 05:27
  • @PSR well it didn't work. only the first string from the list is printed then many red stuff appears (error). which looks like this 2013-05-16 Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 15, Size: 1 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at q$8$1.actionPerformed(q.java:938) – Maguzu May 31 '13 at 05:31
  • this is related to this http://stackoverflow.com/questions/993025/java-adding-elements-to-a-collection-during-iteration – Maguzu May 31 '13 at 06:40