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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

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

 
0
  #1
Feb 23rd, 2008
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:
  1. public class Parse {
  2.  
  3.  
  4.  
  5. public Parse() {
  6. }
  7.  
  8.  
  9. public static void main(String[] args) throws java.io.IOException {
  10.  
  11. String line = null;
  12.  
  13. FileInputStream textfile = new FileInputStream("Customer.txt");
  14. BufferedReader in = new BufferedReader(new InputStreamReader(textfile));
  15. List <Customer> customer = new ArrayList <Customer>();
  16. line = null;
  17. while (null != (line = in.readLine())) {
  18.  
  19. StringTokenizer strtok = new StringTokenizer(line);
  20. while (strtok.hasMoreTokens()) {
  21. Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(strtok.nextToken());
  22. Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(strtok.nextToken());
  23. int i = Integer.parseInt(strtok.nextToken());
  24. //String str = Integer.toString(i);
  25.  
  26.  
  27. Customer s = new Customer(lastModified,lastModified2,i, str);
  28. Customer.add( s);
  29. }
  30. }
  31.  
  32.  
  33.  
  34. }
  35. textfile.close();
  36. }

And thats my customer class:

  1. public class Customer
  2. {
  3. String Name;
  4. double Customer_no;
  5. String post_code;
  6. int telephone_no;
  7. SimpleDateFormat lastModified;
  8.  
  9. public Customer(Date lastModified,
  10. Date lastModified2, int i, String str) {
  11. }
  12. public String getName() {
  13. return Name;
  14. }
  15. public void setName(String name) {
  16. Name = name;
  17. }
  18. public double getCustomer_no() {
  19. return Customer_no;
  20. }
  21. public void setCustomer_no(double customer_no) {
  22. Customer_no = customer_no;
  23. }
  24. public String getPost_code() {
  25. return post_code;
  26. }
  27. public void setPost_code(String post_code) {
  28. this.post_code = post_code;
  29. }
  30. public int getTelephone_no() {
  31. return telephone_no;
  32. }
  33. public void setTelephone_no(int telephone_no) {
  34. this.telephone_no = telephone_no;
  35. }
  36. public SimpleDateFormat getLastModified() {
  37. return lastModified;
  38. }
  39. public void setLastModified(SimpleDateFormat date) {
  40. lastModified = date;
  41. }
  42. public static void add(Customer s) {
  43. }
  44. }



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

Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: BubuJake is an unknown quantity at this point 
Solved Threads: 0
BubuJake BubuJake is offline Offline
Newbie Poster

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

 
0
  #2
Feb 23rd, 2008
It appears you have the wrong case on your ArrayList...

Customer.add( s);

...should read...

customer.add(s);

...Also, you may want to declare your ArrayList like this...

ArrayList <Customer> custlist = new ArrayList <Customer>();

...then change the other line to this...

custlist.add(s);
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

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

 
0
  #3
Feb 24th, 2008
okay i did that, thanks.
but my main concern is how can i make it sure that it will parse correctly all the elements from the text.
  1. public class Parse {
  2.  
  3. public Parse() {
  4. }
  5.  
  6. public static void main(String[] args) throws java.io.IOException {
  7.  
  8. String line = null;
  9. FileInputStream textfile = new FileInputStream("Customer.txt");
  10. BufferedReader in = new BufferedReader(new InputStreamReader(textfile));
  11. ArrayList <Customer> custlist = new ArrayList <Customer>();
  12. line = null;
  13. while (null != (line = in.readLine())) {
  14.  
  15. StringTokenizer strtok = new StringTokenizer(line);
  16. while (strtok.hasMoreTokens()) {
  17. Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(strtok.nextToken());
  18. Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(strtok.nextToken());
  19. int i = Integer.parseInt(strtok.nextToken());
  20. //String str = Integer.toString(i);
  21.  
  22.  
  23. custlist s = new custlist(lastModified,lastModified2,i, str);
  24. custlist.add( s);
  25. }
  26. }
  27.  
  28.  
  29.  
  30. }
  31. textfile.close();
  32. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #4
Feb 24th, 2008
I started to reply yesterday, then decided I would wait to see if someone more knowledgeable replied first since I have not used StringTokenizer much, so code that looks a little odd to me might in fact be correct and I didn't want to say something that may work would not work. No one else has replied yet, so with the disclaimer that I'm not very knowledgeable about StringTokenizer, here goes:

It seems to me that each Customer has five lines of data, with one data attribute per line in the input file. Relevant information about each Customer is after the first colon in each line of the input file. Thus I think you want to have five BufferedReader.readLine commands for each Customer. After reading in a line, throw out everything up through and including the first colon, then do whatever you need to do to convert the String data that is left into the data format that you want for the constructor. Then when you have all five pieces of data the way they need to be, call the Customer constructor, then add that Customer to the ArrayList.

So, again with the disclaimer that I have limited experience using StringTokenizer, I see nothing in you code that throws away everything up through the first colon in every line, and it looks like your are reading one line per Customer, not five. It's not clear to me whether your intent is to execute this line only once per Customer:

  1. while (null != (line = in.readLine()))
but I think:
  1. line = in.readLine ()
needs to occur five times per Customer, not once.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

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

 
0
  #5
Feb 24th, 2008
Okay thanks for the reply
I did the modification but my concern is the parsing of each record
Can you give a look to it ?
Thanks in advance!
  1. public class Parse {
  2.  
  3. public Parse() {}
  4.  
  5. public static void main(String[] args) throws java.io.IOException {
  6.  
  7. String line = null;
  8. FileInputStream textfile = new FileInputStream("Customer.txt");
  9. BufferedReader in = new BufferedReader(new InputStreamReader(textfile));
  10. ArrayList <Customer> custlist = new ArrayList <Customer>();
  11. line = null;
  12. //while (null != (line = in.readLine())) {
  13. line = in.readLine();
  14. StringTokenizer strtok = new StringTokenizer(line);
  15. while (strtok.hasMoreTokens()) {
  16. Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(strtok.nextToken());
  17. Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(strtok.nextToken());
  18. int i = Integer.parseInt(strtok.nextToken());
  19. //String str = Integer.toString(i);
  20.  
  21.  
  22. custlist s = new custlist(lastModified,lastModified2,i, str);
  23. custlist.add( s);
  24. }
  25. }
  26.  
  27.  
  28.  
  29. }
  30. textfile.close();
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #6
Feb 24th, 2008
You still have the same problem, I believe. You are reading one line of data from the file and trying to extract five piece of information from that one line. You can't do that, regardless of how you parse that line. You can't extract a date from a String that only contains a name. I think you need to do this for EACH Customer:

  1. line = in.readLine();
  2. // code to extract name data from "line"
  3. line = in.readLine();
  4. // code to extract cust. # data from "line"
  5. line = in.readLine();
  6. // code to postal code data from "line"
  7. line = in.readLine();
  8. // code to telephone number data from "line"
  9. line = in.readLine();
  10. // code to extract last modified data from "line"
  11.  
  12.  
  13. // call Customer constructor with above extracted data
  14. // add Customer from above to ArrayList
Whether you use StringTokenizer to parse "line" is up to you. There is more than one way. Regardless of how, you need to call readLine five times per Customer, create the new Customer with the data extracted, then add that new Customer to the Array List.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #7
Feb 25th, 2008
VernonDozier is correct, you'll have to read each line separately before attempting to parse its data. StringTokenizer is also considered a legacy class. You should use String.split() or java.util.regex classes instead. You'll need a regex pattern for either one, since your delimiter of ":" also appears in your time values and thus a simple delimeter of ":" will cause those values to get split up and you'll have to merge them back. The Pattern for each of your items is very simple, so either of these methods would work fine
  1. String line = "Last modified: Jan 11, 2007 8:10:05 PM GMT";
  2.  
  3. // using regex Pattern and Matcher
  4. Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)");
  5. Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line);
  6. if (lastModifiedMatcher.matches()){
  7. System.out.println("result: "+lastModifiedMatcher.group(1));
  8. }
  9.  
  10. // using String.split()
  11. if (line.matches("Last modified: (.*)")){
  12. System.out.println("result: "+line.split("Last modified:")[1]);
  13. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

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

 
0
  #8
Feb 26th, 2008
What about now?
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.text.*;
  6. import java.util.StringTokenizer;
  7. import java.text.NumberFormat;
  8.  
  9.  
  10. class Parse {
  11.  
  12. public Parse() {}
  13.  
  14. public static void main(String[] args) throws java.io.IOException {
  15.  
  16. String line = null;
  17. BufferedReader in = new BufferedReader(new FileReader("Customer.txt"));
  18. ArrayList <Customer> custlist = new ArrayList <Customer>();
  19. line = null;
  20. while (null != (line = in.readLine())) {
  21. /*
  22. Name: James Smith
  23. Customer no: 663,282
  24. Post code: BA1 74E
  25. Telephone no: 028-372632
  26. Last modified: Feb 10, 2008 6:50:00 PM GMT
  27. */
  28.  
  29. Pattern namePattern = Pattern.compile("Name: (.*)");
  30. Matcher nameMatcher = namePattern.matcher(line);
  31. if (nameMatcher.matches()){
  32. String name = String.parseString(name);
  33. }
  34.  
  35. Pattern customerNoPattern = Pattern.compile("Customer no: (.*)");
  36. Matcher customerNoMatcher = customerNoPattern.matcher(line);
  37. if (customerNoMatcher.matches()){
  38. int customerNo = Integer.parseInt(customerNo);
  39. }
  40.  
  41.  
  42. Pattern postCodePattern = Pattern.compile("Post code: (.*)");
  43. Matcher postCodeMatcher = postCodePattern.matcher(line);
  44. if (postCodeMatcher.matches()){
  45. String postCode = String.parseString(postCode);
  46. }
  47.  
  48. Pattern telephoneNoPattern = Pattern.compile("Telephone no: (.*)");
  49. Matcher telephoneNoMatcher = telephoneNoPattern.matcher(line);
  50. if (telephoneNoMatcher.matches()){
  51. int telephoneNo = Integer.parseInt(telephoneNo);
  52. }
  53.  
  54. Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)");
  55. Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line);
  56. if (lastModifiedMatcher.matches()){
  57. Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(lastModified);
  58. Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(lastModified2);
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. custlist s = new custlist(lastModified,lastModified2,telephoneNo, name,postCode,customerNo);
  68. custlist.add( s);
  69. }
  70. }
  71.  
  72.  
  73.  
  74. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #9
Feb 26th, 2008
Well, not quite there yet, as I'm sure you noticed if you ran it. Look at the example code I posted with the Matcher. The portion of the match that you want to capture is returned by lastModifiedMatcher.group(1). Group(0) contains the entire text that match the pattern. Group(1) contains the first match group, which is what you want to isolate. Read the API doc on Matcher for more info, but in your case group(1) will return the piece you need.
Last edited by Ezzaral; Feb 26th, 2008 at 4:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

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

 
0
  #10
Feb 26th, 2008
What about now?
  1. class Parse {
  2.  
  3. public Parse() {}
  4.  
  5. public static void main(String[] args) throws java.io.IOException {
  6.  
  7. String line = null;
  8. BufferedReader in = new BufferedReader(new FileReader("Customer.txt"));
  9. ArrayList <Customer> custlist = new ArrayList <Customer>();
  10. line = null;
  11. while (null != (line = in.readLine())) {
  12. /*
  13. Name: James Smith
  14. Customer no: 663,282
  15. Post code: BA1 74E
  16. Telephone no: 028-372632
  17. Last modified: Feb 10, 2008 6:50:00 PM GMT
  18. */
  19.  
  20. Pattern namePattern = Pattern.compile("Name: (.*)");
  21. Matcher nameMatcher = namePattern.matcher(line);
  22. Strng line = in.readLine();
  23. Matcher name = Pattern.matcher(line);
  24. if (nameMatcher.matches()){
  25. String name;
  26. name=group(1);
  27.  
  28. }
  29.  
  30. Pattern customerNoPattern = Pattern.compile("Customer no: (.*)");
  31. Matcher customerNoMatcher = customerNoPattern.matcher(line);
  32. Strng line = in.readLine();
  33. Matcher customerNo = Pattern.matcher(line);
  34. if (customerNoMatcher.matches()){
  35. DecimalFormat format = new DecimalFormat(customerNoPattern).parse(customerNo);
  36. customerNo=group(1);
  37.  
  38. }
  39.  
  40.  
  41. Pattern postCodePattern = Pattern.compile("Post code: (.*)");
  42. Matcher postCodeMatcher = postCodePattern.matcher(line);
  43. Strng line = in.readLine();
  44. Matcher postCode = Pattern.matcher(line);
  45. if (postCodeMatcher.matches()){
  46. String postCode;
  47. postCode=group(1);
  48. }
  49.  
  50. Pattern telephoneNoPattern = Pattern.compile("Telephone no: (.*)");
  51. Matcher telephoneNoMatcher = telephoneNoPattern.matcher(line);
  52. Strng line = in.readLine();
  53. Matcher telephoneNo = Pattern.matcher(line);
  54. if (telephoneNoMatcher.matches()){
  55. String telephoneNo;
  56. telephoneNo=group(1);
  57.  
  58. }
  59.  
  60. //Last modified: Feb 10, 2008 6:50:00 PM GMT????????
  61.  
  62. Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)");
  63. Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line);
  64. Strng line = in.readLine();
  65. Matcher lastModified = Pattern.matcher(line);
  66. Date lastModified = new SimpleDateFormat("Mmm dd, yyyy HH:mm:ss PM GMT").parse(lastModified);
  67. Date lastModified2 = new SimpleDateFormat("Mmm dd, yyyy HH:mm:ss AM GMT").parse(lastModified2);
  68. lastModified=group(1);
  69. lastModified2=group(1);
  70. }
  71.  
  72.  
  73. custlist s = new custlist(name, customerNo, postCode, telephoneNo, lastModified,lastModified2);
  74. custlist.add( s);
  75. }
  76. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 5298 | Replies: 15
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC