Languages :: Java :: Split text and store it in arraylist |
|||
| By: perdoname |
Date: 24/02/2008 08:24:51 |
Points: 20 | Status: Answered Quality : Excellent |
|
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:
My program is that: 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: 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! |
|||
| By: VGR | Date: 24/02/2008 08:33:46 | Type : Answer |
|
$fp=fopen('yourfilename.txt','r'); if($fp!==FALSE) { // work // read a block /* format : 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 (blank line) */ // read, parse, get data $buf=fgets($fp,128); $buf=chop($buf); // strip EOLNs $begin='no: '; $a=strpos($buf,$begin); if ($a===FALSE) die("invalid file format"); $a+=strlen($begin)+1; // extra space $custno=substr($buf,$a); // read, parse, get data $buf=fgets($fp,128); $buf=chop($buf); // strip EOLNs $begin='code: '; $a=strpos($buf,$begin); if ($a===FALSE) die("invalid file format"); $a+=strlen($begin)+1; // extra space $postcode=substr($buf,$a); // etc while ($buf=fgets($fp,128)) { // while not EOF // read a new block // read, parse, get data $buf=fgets($fp,128); $buf=chop($buf); // strip EOLNs $begin='no: '; $a=strpos($buf,$begin); if ($a===FALSE) die("invalid file format"); $a+=strlen($begin)+1; // extra space $custno=substr($buf,$a); // read, parse, get data $buf=fgets($fp,128); $buf=chop($buf); // strip EOLNs $begin='code: '; $a=strpos($buf,$begin); if ($a===FALSE) die("invalid file format"); $a+=strlen($begin)+1; // extra space $postcode=substr($buf,$a); // etc } } else die("coul'nt open file"); personally, I would use a function to "read and parse", but it's up to you. regards PS not tested against typos, especially the argument order for strpos() |
|||
| By: perdoname | Date: 24/02/2008 18:12:15 | Type : Comment |
|
| In what language does your code is ? | |||
| By: VGR | Date: 26/02/2008 21:40:30 | Type : Comment |
|
| PHP, it's compatible with Java (and vice-versa)... it does use only fopen(), fgets(), fclose(), standard C-library routines... is it useful ? |
|||
| By: VGR | Date: 27/02/2008 19:27:41 | Type : Comment |
|
| this said, the main difference between our two codes is that I can't find in your OO overhead stuff the useful part, while mine is directly legible ;-) That's the main difference between OOP (loss of time trying to figure out why it doesn't work as intended) and procedural programming (it works as designed, anyway you code it) probably a very good Java progammer can solve your question in a couple of seconds. I'm only giving away an algorithm that works. Any decent programming language shoud enable you to transmate a good algorithm in a good program in some minutes. regards |
|||
| By: VGR | Date: 01/06/2008 09:59:40 | Type : Comment |
|
| ok ? close Q ? feedback ? | |||
|
Do register to be able to answer |
|||
| Add This Article To: | |||
| |
|
|
|
| |
|
|
|









