Regex Help

I am having trouble with regular expressions, I am really confused by them and can't figure it out. PeriodicTable.java reads values from a file and creates an array of the object Element. It then prompts the user to enter a molecular formula from which it will calculate the atomic mass of the equation. I am getting the following error:

java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:461)
at java.util.regex.Matcher.group(Matcher.java:421)
at PeriodicTable.main(PeriodicTable.java:54)

Any help would be greatly appreciated!

PeriodicTable.java
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class PeriodicTable {
public static void main(String[] args)
throws FileNotFoundException {
int ELEMENTS = 103;
// Windows
//File filePath = new File("c:/ptable.txt");
// Linux
File filePath = new File("/home/jephiroth/ptable.txt");
Element[] elements = new Element[ELEMENTS];
Scanner input = new Scanner(filePath);
// Ignore the first line
String s = input.nextLine();
// Read data from the file
for (int i = 0; i < ELEMENTS; i++) {
s = input.nextLine();
s = s.replaceAll("\t\t", "\t");
String[] fields = s.split("\t");
String name = fields[0];
int number = Integer.parseInt(fields[1]);
String symbol = fields[2];
double weight = Double.parseDouble(fields[3]);
// Create new Element
Element e = new Element(name, symbol, number, weight);
elements[i] = e;
}
String formula = new String();
String element = new String();
int occurance = 0;
double totalWeight = 0.0;
boolean done = true;
Scanner kybrd = new Scanner(System.in);
do {
System.out.println("Please enter your molecular equation (ie. Fe2H2O):");
formula = kybrd.next();
if (formula.equals("QUIT") || formula.equals("quit"))
done = false;
} while (done);

String equation[] = formula.split("[A-Z][a-z]*[1-9]*");
for (int i = 0; i < equation.length; i++) {
Pattern p = Pattern.compile("[A-Z][a-z]*");
Matcher m = p.matcher(equation[i]);
element = m.group();
Pattern q = Pattern.compile("[1-9]*");
Matcher n = q.matcher(equation[i]);
occurance = Integer.parseInt(n.group());
if (occurance == 0)
occurance = 1;
for (int j = 0; j < elements.length; j++) {
if (elements[j].getSymbol() == element)
totalWeight = totalWeight + (elements[j].getWeight() * occurance);
}
System.out.println("Total atomic weight is " + totalWeight);
}
}
}

Element.java
public class Element
{
private String name;
private String symbol;
private int atomicNumber;
private double atomicWeight;

public Element() {
name = "";
symbol = "";
atomicNumber = 0;
atomicWeight = 0;
}
public Element(String eName, String sym, int num, double weight) {
name = eName;
symbol = sym;
atomicNumber = num;
atomicWeight = weight;
}

public String getName() {
return name;
}
public String getSymbol() {
return symbol;
}
public int getNumber() {
return atomicNumber;
}
public double getWeight() {
return atomicWeight;
}
}
[4041 byte] By [authorityaction] at [2007-11-20 11:52:40]
# 1 Re: Regex Help
First of all have you tried reading any regex tutorials such as this one (http://www.regular-expressions.info/tutorial.html) or the Sun one (http://java.sun.com/docs/books/tutorial/essential/regex/).

As for the exception you are getting as you've not told us which is line 54 of the PeriodicTable class it's abit hard to give you any advice other than look at that line and look up the API docs for whichever method(s) are called on that line and the docs will tell you why the exception is being thrown.
keang at 2007-11-10 2:14:02 >
# 2 Re: Regex Help
I suggest you learn how to use Matcher. The API docs say:

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown. The explicit state of a matcher is recomputed by every match operation.

Computers are good at following instructions, but not at reading your mind...
D. Knuth
dlorde at 2007-11-10 2:15:02 >
# 3 Re: Regex Help
I have read the tutorials and now have a better understanding of what I need to do. I have fixed the error that I was getting but now I am having trouble getting the exact regular expression to work. I need to split up a string like Fe2H2O into Fe2 H2 O but I can't get the right regular expression. Then from there I need to split it up further to Fe 2 H 2 O. Does that make sense?
authorityaction at 2007-11-10 2:16:01 >
# 4 Re: Regex Help
heres a nice extensive explanation of java regex and matcher use: http://mindprod.com/jgloss/regex.html
Xeel at 2007-11-10 2:17:00 >
# 5 Re: Regex Help
I figured out what I was doing wrong and ultimately learned a great deal about regular expressions. Thanks for the help and sorry if I was confusing, it was hard to explain something I knew nothing about.
authorityaction at 2007-11-10 2:17:57 >