Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

Exception in thread "main" java.lang.IllegalStateException: ??

Hello Experts,

I need your assistance about the following error:
Exception in thread "main" java.lang.IllegalStateException: No match found
      at java.util.regex.Matcher.group(Matcher.java:461)
      at Parse.<init>(Parse.java:75)
      at Parse.main(Parse.java:26)


Thanks in advance for any help !
//PARSE CLASS:
 
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Parse {
 
	ArrayList <Customer> custlist = new ArrayList <Customer>();
 
 
	public static void main(String[] args) throws java.io.IOException, NumberFormatException, ParseException {
 
		new Parse();
	}
 
	public Parse() throws ParseException, NumberFormatException, IOException
	{
		String line = null;
		BufferedReader in = new BufferedReader(new FileReader("Customers.txt"));
		Pattern namePattern = Pattern.compile("Name: (.*)");
		Pattern customerNoPattern = Pattern.compile("Customer no: (.*)");
		Pattern postCodePattern = Pattern.compile("Post code: (.*)");
		DecimalFormat nf = new DecimalFormat("ddd");
		DateFormat df = DateFormat.getDateInstance();
		line = null;
		while (null != (line = in.readLine())) {
 
			String name = null;
			Matcher nameMatcher = namePattern.matcher(line);
			if (nameMatcher.matches()){
				name = nameMatcher.group(1);
			}
 
			String customerNumber = null;
			double customerNo = 0;
			Matcher customerNoMatcher = customerNoPattern.matcher(line);
			if (customerNoMatcher.matches()){
				customerNumber = customerNoMatcher.group(1);
				customerNo = Double.parseDouble(customerNumber);
 
			}
 
 
			String postCode=null;;
			Matcher postCodeMatcher = postCodePattern.matcher(line);
			if (postCodeMatcher.matches()){
				postCode = postCodeMatcher.group(1);
			}
 
			String telephone = null;
			int telephoneNo = 0;
			Pattern telephoneNoPattern = Pattern.compile("Telephone no: (.*)");
			Matcher telephoneNoMatcher = telephoneNoPattern.matcher(line);
			if (telephoneNoMatcher.matches()){
				telephone = telephoneNoMatcher.group(1);
				telephoneNo = Integer.parseInt(telephone);
			}
 
			String lastdate = null;
			Pattern lastModifiedPattern = Pattern.compile("Last modified: (.*)");
			Matcher lastModifiedMatcher = lastModifiedPattern.matcher(line);
			lastdate = lastModifiedMatcher.group(1);
			Date lastModified = df.parse(lastdate);
 
			Customer s = new Customer(name, customerNo, postCode, telephoneNo, lastModified);
			custlist.add( s);
		}
 
	}
 
 
	void fmtDateNumber(Locale loc) throws IOException
	{
		FileWriter fw;
		fw = null;
		try {
			fw = new FileWriter("Customers" + loc.getDisplayCountry() + ".txt");
		} catch (final IOException ioe) {
			System.out.println("Error");
		}
		final NumberFormat numfmt = NumberFormat.getNumberInstance(loc);
		final DateFormat datafmt = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, loc);
		for (final Customer c: custlist) {
			fw.write("Name: " + c.getName()+"\n");
			fw.write("Customer no: " + numfmt.format(c.getCustomerNo())+"\n");
			fw.write("Post code: " + c.getPostCode()+"\n");
			fw.write("Telephone no: " + c.getTelephoneNo()+"\n");
			fw.write("Last modified: " + datafmt.format(c.getLastModified())+"\n");
		}
		try {
			fw.close();
		} catch (final IOException ioe) {
			System.out.println("Error");
		}
	}
 
	void fmtDateNumberBinary(Locale loc) throws IOException
	{
 
		FileOutputStream out = new FileOutputStream("Customers" + loc.getDisplayCountry() + ".dat");
		ObjectOutputStream s = new ObjectOutputStream(out);
 
		final NumberFormat numfmt = NumberFormat.getNumberInstance(loc);
		final DateFormat datafmt = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, loc);
		for (final Customer c: custlist) {
			s.writeObject("Name: " + c.getName()+"\n");
			s.writeObject("Customer no: " + numfmt.format(c.getCustomerNo())+"\n");
			s.writeObject("Post code: " + c.getPostCode()+"\n");
			s.writeObject("Telephone no: " + c.getTelephoneNo()+"\n");
			s.writeObject("Last modified: " + datafmt.format(c.getLastModified())+"\n");
 
			out.close();
			s.close();
		}
	}
 
 
 
 
	public void saveIndex()
	{
		ObjectOutputStream output;
 
		try
		{
			output = new ObjectOutputStream(new FileOutputStream( "Customers.index" ) );
			output.writeObject(custlist);
			output.close();
		}
		catch ( IOException ioException )
		{
			System.err.println( "Error opening file" );
		}
 
	}
 
 
 
}
 
 
 
 
 
 
 
import java.util.Date;
 
 
//CUSTOMER CLASS:
 
class Customer
{
 
 
	private String name;
	private double customerNo;
	private String postCode;
	private int telephoneNo;
	private Date lastModified;
	public Customer( String name,double customerNo, String postCode,int telephoneNo, Date lastModified)
	{
		this.name=name;
		this.customerNo=customerNo;
		this.postCode=postCode;
		this.telephoneNo=telephoneNo;
		this.lastModified=lastModified;
	}
 
 
	public String getName() {
		String name = null;
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getCustomerNo() {
		double customerNo = 0;
		return customerNo;
	}
	public void setCustomerNo(double customerNo) {
		this.customerNo = customerNo;
	}
	public String getPostCode() {
		String postCode = null;
		return postCode;
	}
	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	public int getTelephoneNo() {
		int telephoneNo = 0;
		return telephoneNo;
	}
	public void setTelephoneNo(int telephoneNo) {
		this.telephoneNo = telephoneNo;
	}
	public Date getLastModified() {
		Date lastModified = null;
		return lastModified;
	}
	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}
 
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't call group if there's no match. I've already posted code that does this for you in a much simpler way
you can check if there is a match before calling group,

matcher.find()
Avatar of perdoname_
perdoname_

ASKER

The point is that i cant use that code because it parses a country-specific version date only :s

But i cant understand why it returns the error it should be a match :S
Thats a part of the text file :

Name: Sarah Thomas
Customer no: 96,343
Post code: BA1 5BA
Telephone no: 0125-9423349
Last modified: Jun 12, 2006 10:50:00 AM GMT

>>The point is that i cant use that code because it parses a country-specific version date only :s

Not quite sure what you mean there, but the date parsing is of course changeable
ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>The point is that i cant use that code because it parses a country-specific version date only :s

As it happens, the date in your example is not parseable with a UK Locale, which i'm guessing is the one you're using