Why i'm i getting this errors?
Java.io.FileNotFoundException:File.dat(The system cannot find the file specified )
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at ReadInteger.<init>(ReadInteger.java:27)
at TestFile.main(TestFile.java:14)
Here's my code:
import java.io.*;
public class ReadInteger
{
private File f;
private FileInputStream fis;
private InputStreamReader isr;
private BufferedReader br;
final static int maxArrayLength = 100;
private int [] xIntegers;
private int numOfValues;
private double average;
private
String inputString = new String();
boolean eofFlag = false;
//Default constructor
public ReadInteger(String fileName)
{
xIntegers = new int[maxArrayLength];
numOfValues = 0;
f = new File(fileName);
fis = null;
try{
fis = new FileInputStream(f);
}catch(IOException ioe){
ioe.printStackTrace();
System.err.println("Error while reading input from file");
System.exit(1);
}
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
}
public void readValues()
{
System.out.println("Enter the number of values in the array");
try{
inputString = br.readLine();
numOfValues = Integer.parseInt(inputString);
xIntegers = new int [numOfValues];
}catch(IOException e){
System.err.println("can't read input");
System.exit(1);
}
//prompts the user for inputs
for(int i = 0; i <numOfValues; i++)
{
System.out.println("Please enter a number");
try{
inputString = br.readLine();
xIntegers[i] = Integer.parseInt(inputString);
}catch(IOException e){
System.err.println("can't read input");
System.exit(2);
}
}
//This block automatically calculates the average of an array of integers
if (xIntegers.length > 0)
{
for (int i = 0; i < numOfValues; ++i)
average += xIntegers[i];
average = average/xIntegers.length;
}//end of average block
}
/**This methods writes out the values in the array of numbers
* and also prints the average and standard deviation of the numbers*/
public void writeValues()
{
System.out.println("The numbers in the array are:");
for(int i= 0; i<numOfValues; i++)
{
System.out.println("[" + i +"] = " + xIntegers[i]);
}
System.out.println("The Average is" + " " + average);
System.out.println();
System.out.println("The Standard Deviation is" + " " +
calStandardDev(xIntegers,average));
System.out.println();
}//end of writeValues()
/**This method gets and returns the number
of values in an array of numbers*/
public int getNumOfValues()
{
return numOfValues;
}
//Gets and returns the average
public double getAverage()
{
return average;
}
/**This method calculates the standard deviation
of an array of numbers and returns it*/
public double calStandardDev(int [] xIntegers, double average)
{
double sd = 0.0;
if(xIntegers.length >0)
{
average = getAverage();
for (int i = 0; i < xIntegers.length; i++)
{
sd += Math.pow(xIntegers[i] - average,2);//raises sd to the power of two
}
sd = Math.sqrt(sd/(xIntegers.length -1));
}
return sd;
}
}
//Main
import java.io.*;
public class TestFile
{
/**THE MAIN PROGRAM STARTS HERE: CALLS ALL
*APPROPRIATE METHODS IN THE ARRAY CLASS TO
*DO NECESSARY THE COMPUTATIONS AND PRINTS
*THE RESULT **/
public static void main (String []args)
{
ReadInteger ta = new ReadInteger("File.dat");
ta.readValues();
ta.writeValues();
}
}//end of
test class