Edit Adapted code since then. Would still like to eliminate repeated bits of code... Cannot think how.
public static void readFile(String fileName) throws IOException, FileNotFoundException{
double fileX;
double fileY;
double fileZ;
// Reference the file using the the BufferedReader object
BufferedReader input = new BufferedReader(new FileReader(fileName));
//scanner to scan through file
Scanner token = new Scanner(input);
/*
* skipToDouble() method makes it possible to search my file for double values
*/
// initialize x, y and z to place inside vector A
fileX= skipToDouble(token);
fileY= skipToDouble(token);
fileZ= skipToDouble(token);
vecA = new Vector3D(fileX,fileY,fileZ);
// initialize x, y and z to place inside vector B
fileX= skipToDouble(token);
fileY= skipToDouble(token);
fileZ= skipToDouble(token);
vecB = new Vector3D(fileX, fileY, fileZ);
// initialize x, y and z to place inside vector C (dummy vector really)
fileX= skipToDouble(token);
fileY= skipToDouble(token);
fileZ= skipToDouble(token);
//initialize vecC using double values from third line of file
vecC = new Vector3D(fileX, fileY, fileZ);
//close file
input.close();
}
//keeps looking until double is found
private static double skipToDouble(Scanner scanner) {
while (scanner.hasNext() && !scanner.hasNextDouble()) {
scanner.next();
}
return scanner.hasNextDouble() ? scanner.nextDouble() : Double.NaN;
}
EDIT: Trying the suggestion I am not getting a NullInputException and cannot see why:
// Reference the file using the the BufferedReader object
BufferedReader input = new BufferedReader(new FileReader(fileName));
//scanner to scan through file
Scanner tokens = new Scanner(input);
for(int i=0; i<3; i++){
System.out.print("i = " + i);
for(int j=0; j<3; j++){
System.out.print("j = " + j);
file[j]=skipToDouble(tokens);
}
vec[i]= new Vector3D(file[0], file[1], file[2]);
}
//close file
input.close();
readNextDouble
andreadNextVector
using these two methods. – Sulthan Oct 8 '12 at 8:44