| | |
Split text and store it in arraylist [ problem with code]
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
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:
And thats my customer class:
If anyone could help me completing my program/clarifying errors it 'd be grateful !
Thanks in advance!
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:
java Syntax (Toggle Plain Text)
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)); List <Customer> customer = new ArrayList <Customer>(); 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); Customer s = new Customer(lastModified,lastModified2,i, str); Customer.add( s); } } } textfile.close(); }
And thats my customer class:
java Syntax (Toggle Plain Text)
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!
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
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.
but my main concern is how can i make it sure that it will parse correctly all the elements from the text.
java Syntax (Toggle Plain Text)
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 <Customer> custlist = new ArrayList <Customer>(); 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(); }
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
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:
but I think:
needs to occur five times per Customer, not once.
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:
Java Syntax (Toggle Plain Text)
while (null != (line = in.readLine()))
Java Syntax (Toggle Plain Text)
line = in.readLine ()
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
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!
I did the modification but my concern is the parsing of each record
Can you give a look to it ?
Thanks in advance!
Java Syntax (Toggle Plain Text)
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 <Customer> custlist = new ArrayList <Customer>(); line = null; //while (null != (line = in.readLine())) { 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(); }
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
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:
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.
Java Syntax (Toggle Plain Text)
line = in.readLine(); // code to extract name data from "line" line = in.readLine(); // code to extract cust. # data from "line" line = in.readLine(); // code to postal code data from "line" line = in.readLine(); // code to telephone number data from "line" line = in.readLine(); // code to extract last modified data from "line" // call Customer constructor with above extracted data // add Customer from above to ArrayList
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
java Syntax (Toggle Plain Text)
String line = "Last modified: Jan 11, 2007 8:10:05 PM GMT"; // using regex Pattern and Matcher Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)"); Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line); if (lastModifiedMatcher.matches()){ System.out.println("result: "+lastModifiedMatcher.group(1)); } // using String.split() if (line.matches("Last modified: (.*)")){ System.out.println("result: "+line.split("Last modified:")[1]); }
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
What about now?
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.text.*; import java.util.StringTokenizer; import java.text.NumberFormat; class Parse { public Parse() {} public static void main(String[] args) throws java.io.IOException { String line = null; BufferedReader in = new BufferedReader(new FileReader("Customer.txt")); ArrayList <Customer> custlist = new ArrayList <Customer>(); line = null; while (null != (line = in.readLine())) { /* Name: James Smith Customer no: 663,282 Post code: BA1 74E Telephone no: 028-372632 Last modified: Feb 10, 2008 6:50:00 PM GMT */ Pattern namePattern = Pattern.compile("Name: (.*)"); Matcher nameMatcher = namePattern.matcher(line); if (nameMatcher.matches()){ String name = String.parseString(name); } Pattern customerNoPattern = Pattern.compile("Customer no: (.*)"); Matcher customerNoMatcher = customerNoPattern.matcher(line); if (customerNoMatcher.matches()){ int customerNo = Integer.parseInt(customerNo); } Pattern postCodePattern = Pattern.compile("Post code: (.*)"); Matcher postCodeMatcher = postCodePattern.matcher(line); if (postCodeMatcher.matches()){ String postCode = String.parseString(postCode); } Pattern telephoneNoPattern = Pattern.compile("Telephone no: (.*)"); Matcher telephoneNoMatcher = telephoneNoPattern.matcher(line); if (telephoneNoMatcher.matches()){ int telephoneNo = Integer.parseInt(telephoneNo); } Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)"); Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line); if (lastModifiedMatcher.matches()){ Date lastModified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(lastModified); Date lastModified2 = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd").parse(lastModified2); } custlist s = new custlist(lastModified,lastModified2,telephoneNo, name,postCode,customerNo); custlist.add( s); } } }
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.
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
What about now?
java Syntax (Toggle Plain Text)
class Parse { public Parse() {} public static void main(String[] args) throws java.io.IOException { String line = null; BufferedReader in = new BufferedReader(new FileReader("Customer.txt")); ArrayList <Customer> custlist = new ArrayList <Customer>(); line = null; while (null != (line = in.readLine())) { /* Name: James Smith Customer no: 663,282 Post code: BA1 74E Telephone no: 028-372632 Last modified: Feb 10, 2008 6:50:00 PM GMT */ Pattern namePattern = Pattern.compile("Name: (.*)"); Matcher nameMatcher = namePattern.matcher(line); Strng line = in.readLine(); Matcher name = Pattern.matcher(line); if (nameMatcher.matches()){ String name; name=group(1); } Pattern customerNoPattern = Pattern.compile("Customer no: (.*)"); Matcher customerNoMatcher = customerNoPattern.matcher(line); Strng line = in.readLine(); Matcher customerNo = Pattern.matcher(line); if (customerNoMatcher.matches()){ DecimalFormat format = new DecimalFormat(customerNoPattern).parse(customerNo); customerNo=group(1); } Pattern postCodePattern = Pattern.compile("Post code: (.*)"); Matcher postCodeMatcher = postCodePattern.matcher(line); Strng line = in.readLine(); Matcher postCode = Pattern.matcher(line); if (postCodeMatcher.matches()){ String postCode; postCode=group(1); } Pattern telephoneNoPattern = Pattern.compile("Telephone no: (.*)"); Matcher telephoneNoMatcher = telephoneNoPattern.matcher(line); Strng line = in.readLine(); Matcher telephoneNo = Pattern.matcher(line); if (telephoneNoMatcher.matches()){ String telephoneNo; telephoneNo=group(1); } //Last modified: Feb 10, 2008 6:50:00 PM GMT???????? Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)"); Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line); Strng line = in.readLine(); Matcher lastModified = Pattern.matcher(line); Date lastModified = new SimpleDateFormat("Mmm dd, yyyy HH:mm:ss PM GMT").parse(lastModified); Date lastModified2 = new SimpleDateFormat("Mmm dd, yyyy HH:mm:ss AM GMT").parse(lastModified2); lastModified=group(1); lastModified2=group(1); } custlist s = new custlist(name, customerNo, postCode, telephoneNo, lastModified,lastModified2); custlist.add( s); } }
![]() |
Other Threads in the Java Forum
- Previous Thread: method help
- Next Thread: Need refactoring opinions
Views: 5298 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes clear client code codesnippet component database db development dice draw ebook eclipse error event exception file formatingtextintooltipjava fractal game givemetehcodez graphics gui helpwithhomework html ide image infinite input integer invokingapacheantprogrammatically j2me jarfile java javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object openjavafx oracle parameter print problem program programming project recursion repositories scanner screen scrollbar server set size sms socket sort sorting sql sqlserver state storm string superclass swing swt test text-file threads time transfer tree windows






