New To Java - Illegal start of expression (try-catch)
Well I'm working on this program that gives you the roots to a quadratic equation when you grab the coefficients from a file, and have it print the roots in another file. The problem I'm having is getting the program to notice when the specified file (in or out) is missing, and have it print an error message. I've commented what i'm trying to use... any suggestions would rock pretty hard. I apologize ahead of time for breaking the table of the form... o.O
import java.io.*;
class Assignment4a{
BufferedReader inFile =new BufferedReader(new FileReader("coeffs.dat"));// preparing to read from file
/* I would like to substitute the above two lines with the code included in this comment, however I recieve
a "Illegal start of expression error",which then points to 'try' and the line which includes "coeffs.dat".
I was unable to find anywhere that try-catch statements are required in methods only, but thats the only
thing I could think of. Any suggestions you have on the matter would be greatly appriciated. Desired code is as follows:
try {
BufferedReader inFile = new BufferedReader(new FileReader("coeffs.dat"));
}
catch (FileNotFoundException fnfe) {
System.out.println("Please confirm that you have coeffs.dat in the same dirrectory where you're running this program... then try again.");
System.exit(0);
}
*/
PrintWriter outFile =new PrintWriter (new FileWriter("out.dat",true));// preparing to write to file
Assignment4a(int number_of_equations)throws IOException{
for (int i = 0; i < number_of_equations ; i++){// loop to run amount of times enterd from keyboard
double a = readCoeffs(number_of_equations);double b = readCoeffs(number_of_equations);double c = readCoeffs(number_of_equations);
if (a != 0){// to check and be sure a does not equal zero, leading to a division by zero
if ( discr(a,b,c) >= 0 ){// checking to see if discriminant is non-negative
double x1 = (-b - Math.sqrt(discr(a,b,c)))/2*a;//calculating roots
double x2 = (-b + Math.sqrt(discr(a,b,c)))/2*a;
outFile.println("\nFor coefficiants: a=" + a +": b=" + b +": and c=" + c +",");
outFile.println("You recieve the following roots: " + x1 +" and " + x2 +"\n");
}
else{
outFile.println("\nFor coefficiants: a=" + a +": b=" + b +": and c=" + c +", \nyou have a negative discriminate. Answer is not in the real domain");
}
}
else{
outFile.println("\nEquation #" + (i+1) +" can not have an 'a' value of zero");
}
}
outFile.close();
}
double readCoeffs(int methcount)throws IOException{
String temp =null;// initiating internal variable
try{
temp = inFile.readLine();
}
catch (FileNotFoundException e){
System.out.println("\nPlease create coeffs.dat containing your data in the dirrectory where this program is located... then try again");
System.exit(0);
}
double value = 0.0;//initiating internal variable
if (temp ==null){
outFile.println("\nEquation #" + (methcount+1) +" does not a suficiant amount of data to run. Add needed data and retry.");
outFile.close();
System.exit(0);
}
try{
value = Double.parseDouble(temp);// checking to see if what was pulled from file is a number
}
catch (NumberFormatException numformat){// resulting steps if one of the entries is not a number
outFile.println("\nPlease be sure all of your coeffciants are integers, then try again");
outFile.close();
System.exit(0);
}
return value;
}
double discr(double a,double b,double c){
return b*b-4*a*c;
}
publicstaticvoid main(String args[])throws IOException{
try{
new Assignment4a(Integer.parseInt(args[0]));
}
catch (ArrayIndexOutOfBoundsException outbounds){
new Assignment4a(1);// 1 will be passed to counter if user forgets to enter in number of times the program is run from the command line
}
}
}

