Split text and store it in arraylist [ problem with code]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perdoname
    New Member
    • Feb 2008
    • 7

    #1

    Split text and store it in arraylist [ problem with code]

    Hello,

    Im trying to implement a program which will split a text file and then parses the elements to an arraylist.
    My text file looks like that:

    Name: Mariah Johnson
    Customer no: 663,283
    Post code: BA1 74E
    Telephone no: 028-372632
    Last modified: Jan 11, 2007 8:10:05 PM GMT

    Name: Jim Blahblah
    Customer no: 863,483
    Post code: BA1 64B
    Telephone no: 011-342349
    Last modified: Jan 27, 2008 1:56:00 AM GMT
    My program is that:
    Code:
    public class Parse {
    
    public Parse() {
    }
    
    public static void main(String[] args) throws java.io.IOException {
    
    String line = null;
    FileInputStream textfile = new FileInputStream("Customer.txt");
    BufferedReader in = new BufferedReader(new InputStreamReader(textfile));
    ArrayList custlist = new ArrayList ();
    line = null;
    while (null != (line = in.readLine())) {
    
    StringTokenizer strtok = new StringTokenizer(line);
    while (strtok.hasMoreTokens()) {
    Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(strtok.nextToken());
    Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(strtok.nextToken());
    int i = Integer.parseInt(strtok.nextToken());
    //String str = Integer.toString(i);
    
    
    custlist s = new custlist(lastModified,lastModified2,i, str);
    custlist.add( s);
    }
    }
    
    
    
    }
    textfile.close();
    }
    And thats my customer class:


    Code:
    public class Customer
    {
    String Name;
    double Customer_no;
    String post_code;
    int telephone_no;
    SimpleDateFormat lastModified;
    
    public Customer(Date lastModified,
    Date lastModified2, int i, String str) {
    }
    public String getName() {
    return Name;
    }
    public void setName(String name) {
    Name = name;
    }
    public double getCustomer_no() {
    return Customer_no;
    }
    public void setCustomer_no(double customer_no) {
    Customer_no = customer_no;
    }
    public String getPost_code() {
    return post_code;
    }
    public void setPost_code(String post_code) {
    this.post_code = post_code;
    }
    public int getTelephone_no() {
    return telephone_no;
    }
    public void setTelephone_no(int telephone_no) {
    this.telephone_no = telephone_no;
    }
    public SimpleDateFormat getLastModified() {
    return lastModified;
    }
    public void setLastModified(SimpleDateFormat date) {
    lastModified = date;
    }
    public static void add(Customer s) {
    } 
    }

    If anyone could help me completing my program/clarifying errors it 'd be grateful !

    Thanks in advance!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    StringTokenizer is old. Use the String.split method instead.

    Comment

    Working...