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:

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:

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!

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);

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.

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();
		}

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:

while (null != (line = in.readLine()))

but I think:

line = in.readLine ()

needs to occur five times per Customer, not once.

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!

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();
		}

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:

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

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.

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

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]);
}

What about now?

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.

What about now?

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);
			     }
			   }

Well, does it compile? Have you tried to fix the errors? You nearly have it correct, but there are still some pieces of the example code that you have not quite gotten.

You would gain more by playing the "What about now?" game with the compiler and the program outputs.

I tried it compiling it through eclipse but it returns error such as missing method for the .group(1) and such as that things.
Thats why i did ask you ... :-/

Of course it does - because you are not calling the methods on the matcher you created, as per the example, and you need to obtain the matcher from the compiled pattern, not with Pattern.matcher().

You need to work through those errors. The compiler messages are pretty straightforward and you almost have it. This is just very basic debugging.

So do i have to create a group method each time ?

You do not "create" a group method at all. It is a method on the Matcher, which resulted from calling the matcher() method on the pattern with the text that you want to apply the pattern to. If the pattern matches, the group(int) method is used to access the data from the pattern's capturing groups (you pattern only has one, which is the "(.*)" part).

String line = "Last modified: Jan 11, 2007 8:10:05 PM GMT";

// compile the pattern to be used
Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)");
// obtain a Matcher for that pattern on the target text
Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line);
// see if the pattern matches the text
if (lastModifiedMatcher.matches()){
    // get the data that matched certain portions of the pattern
    // in your case, this will be in group 1
    String thePieceYouWant = lastModifiedMatcher.group(1));
}

I would recommend reading the api docs for Pattern and Matcher carefully if you wish to understand the nuances of how this code works. Regex is very powerful and provides a lot of flexibility in parsing all kinds of data out of text, but with that comes a level of complexity that requires a bit of study.

So now it should be concerned as completed in the part of parsing ? isn't it ?

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);
					String line = in.readLine();
			    	Matcher name = Pattern.matcher(line);
					if (nameMatcher.matches()){
						String name;
					    String name = nameMatcher.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);
			    		String customerNo = customerNoMatcher.group(1);
			   

			    	}
			    	
				       
			    	Pattern postCodePattern = Pattern.compile("Post code: (.*)");
			    	Matcher postCodeMatcher = postCodePattern.matcher(line);
			    	String 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;
			    		String telephoneNo = telephoneNoMatcher.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);
		    		
				    String lastModified = lastModifiedMatcher.group(1);
				    String lastModified2 = lastModifiedMatcher.group(1);

				}

			       custlist s = new custlist(name, customerNo, postCode, telephoneNo, lastModified,lastModified2);
			       custlist.add( s);
			     }

	private static String group(int i) {
		return null;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.