Regex Help
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;
}
}

