Results 1 to 3 of 3
  1. #1
    Jdelacroix is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Using the split() method in the string class

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;

    public class Assignment {
    public static void main(String[] args) throws IOException{
    File file = new File ("data.csv");
    Scanner inputFile = new Scanner (file);

    inputFile.nextLine();
    ArrayList <Integer> firstLine = new ArrayList <Integer>();
    String str = inputFile.nextLine();
    System.out.print(str);
    //firstLine.add();
    String [] first = new String [50];
    first [0] = new str.split(",",1);

    }
    }
    I am trying tom put the data in the csv file (comma separated) into an array list for each line. I have skipped over the first line since it is just the title for each category. How can I use the split() method in the string class to store the data from the file in arraylists?

  2. #2
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    String array[]=str.split(",");

    then loop over the array and put it on your arraylist
    Keep Moving Forward

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nap_patague View Post
    String array[]=str.split(",");

    then loop over the array and put it on your arraylist
    No need to loop:

    Java Code:
    String[] array= s.split( ... );
    List arrayList= new ArrayList(Arrays.asList(array));
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. String split method
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 02:01 PM
  2. String split method
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 11:06 PM
  3. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 06:15 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •