6 Replies Latest reply: Mar 2, 2007 8:10 AM by 800308 RSS

    splitting an ArryList to produce two other ArrayLists

    807599
      Hello, I am still strying to figure this out. Thanks for the replies to the previous post. So I have an ArrayList now:
       ll = [New Mexcio/Albuquerque
      , California/Los Angeles, Illinois/Chicago, New York/New York, Arizona/Phoenix, Oregon/Portland]
      It consists of 6 entries and the goal is to make two separate ArrayLists holding states and cities separately. My idea was to iterate through the first ArrayList and do another split for each entry, putting the two tokens into two different ArrayLists each time.. I was trying to do all that in one for each loop only, but I dont think there is a way. I am copying the code, which throws a runtime exception:
      java.util.NoSuchElementException
      ... Any ideas welcome..

      import java.util.*;
      import java.io.*;
      public class ScannerFile { 
          public static void main(String[] args) throws Exception {
              Scanner sc = new Scanner(new File("statecity.txt"));
              sc.useDelimiter("\n");
              List<String> ll = new ArrayList<String>();
              List<String> st = new ArrayList<String>(); //hold states only
              List<String> ct = new ArrayList<String>(); //hold cities only
            while (sc.hasNext()) {
              ll.add(sc.next());   
            }
              System.out.println(ll); //just printing
              System.out.println("    " +ll.size());//just printing
            for(String s:ll){
                Scanner ss = new Scanner(s); //Second Scanner to 
                 ss.useDelimiter("/");
                  while (ss.hasNext()) {
                  st.add(sc.next());   
                  }
                System.out.println(s); //Just printing the entries..Is further split possible in here?
            }    
          } 
      }