I think I can help you but I'm pretty amateur at this. I wrote code which essentially does the same thing and it works (with glitches I haven't gotten around to fixing yet). I highly doubt I used the best tools in the Java API. Regardless I'll show you basically what I did.
My strategy was to load the text data into a BufferedReader. A BufferedReader is just a fancy data type that takes the contents of a text file and loads into RAM. Next I grabbed all of the characters in between the spaces and new lines and processed them.
With this strategy your polynomials will not be given nice names like P1 and P2. your polynomials will be loaded into an array of polynomials. so you will need to use an array index number instead.
this code requires that the text file containing the polynomial's information be set up special. It assumes that it will put an integer exponent value followed by a space followed by a double coefficient value. It also assumes that each line defines a different polynomial. Here's an example:
3 0.5 2 4.12 1 8 12 3.3
2 16.15 1 13.2 -1 2
A text file containing only those numbers exactly as shown should load the following two polynomials:
p1 = 0.5^3 + 4.12^2 + 8 + 3.3^12
p2 = 16.5^2 + 13.2 + 2^-1
public static PolynomialTest[] loadLevelFromFile(String polyFileLocation) throws IOException{
File polyFile = new File(polyFileLocation);
return loadFromFile(polyFile);
}
public static PolynomialTest[] loadFromFile(File polyFile) throws IOException{
int i, j, k; //for indexing
i = k = 0; j = 1; //i and j are for finding the terms, k is for keeping track of the current ArrayList index
// a container that can hold as many polynomials as you want
ArrayList<PolynomialTest> thePolys = new ArrayList<PolynomialTest>();
//takes the file object, which is just a file location on the mechine, and loads its contents into ram
BufferedReader polyFileReader = new BufferedReader(new FileReader(polyFile));
//takes the first line of that file and puts it in the string called currentLine
String exponent, coefficient; // for the character sequence between spaces and new lines
String currentLine = polyFileReader.readLine();
while (!(currentLine == null)){
while( !currentLine.substring(j-1, j).matches(" ")){
if (j < currentLine.length()){
j = j + 1;
}else{
break;
}
}
exponent = currentLine.substring(i , j);
i = j;
while( !currentLine.substring(j-1, j).matches(" ")){
if (j < currentLine.length()){
j = j + 1;
}else{
break;
}
}
coefficient = currentLine.substring(i , j);
thePolys.add(new PolynomialTest());
thePolys.get(k).addTerm(Integer.parseInt(exponent), Double.parseDouble(coefficient));
currentLine = polyFileReader.readLine();
}
PolynomialTest[] result = (PolynomialTest[]) thePolys.toArray();
return result;
}
I did not test this code and I wrote it rather fast but it does show the concept. It most likely has bugs. My implementation of similar code required that each line ended with a space and that the entire text document ended with a blank line.
If you have a copy of Bruce Eckel's thinking in Java 4th edition laying around I recommend you look through the chapter on I/O( that's where I learned ).If you don't have a copy, the author has the third edition on his website, free to download. You can find a download link here:
Bruce Eckel's Free Electronic Books