• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading integer from a file

 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Ess
Bartender
Pie
Posts: 9257
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by unyime inok:
Why i'm i getting this errors?
Java.io.FileNotFoundException:File.dat(The system cannot find the file specified )


Because "The system cannot find the file specified".
Is File.dat in the current directory? You can check by using File.exists().
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, i'm supposed to create the file.
 
Joe Ess
Bartender
Pie
Posts: 9257
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So make the file.
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i go about making a file?
 
Joe Ess
Bartender
Pie
Posts: 9257
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question more than once. You end up wasting other people's time, like Jeff, who have missed the conversation so far.
As for your question, you can create a text file with the same text editor you are using to edit your Java source code.
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did that already, but i still get the same error. the file i'm trying to open is in the same directory and i used the same editor to create it.
 
Joe Ess
Bartender
Pie
Posts: 9257
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say that that is very important information that both I and Jeff would have liked to know before we spent any more time on your problem.
I tried your latest code and it does not compile. Get it to compile and let us know what the result is. If you still have a problem, post your latest code.

[ November 03, 2004: Message edited by: Joe Ess ]
[ November 03, 2004: Message edited by: Joe Ess ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do post your latest code, please be sure to format it using the CODE UBB tags to bracket your code. Doing so will preserve the indenting and will make me--and other JavaRanch folks--much more willing to help out. When long code segments are not formatted I usually ignore the post, and I don't think I'm the only one.

Also, as Joe properly pointed out, please do not post the same question more than once as you waste people's valuable time when they answer a post that may have already been answered.
[ November 03, 2004: Message edited by: Jeff Bosch ]
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create a text file and rename it to File.dat

If it still not working use


FileInputStream fis = new FileInputStream("Out.txt");
DataInputStream dis = new DataInputStream(fis);
while(true){
char c =dis.readChar();
System.out.println(c);
}
}catch(IOException ioe) {}
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that new String() is of no use. Why? Once a String object is created you cannot change the String object. Only thing you can do is change the reference value of the object which is in the variable.
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your responses
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic