PDA

View Full Version : I don't understand why this isn't working



Petoux
28th January 2007, 06:08 PM
/* Class RainFall calculates the average rainfall over a period
of days. The number of days and the rain statistics are in
file Rain.in */

import java.io.*;

public class RainFall
{
// Declare dataFile for input
private static BufferedReader dataFile;

static double getInches(BufferedReader dataFile, int numberDays)
throws IOException
// Reads and returns the total inches of rain
{
double total = 0.0;
double inches;
int days = 1;
while (days <= numberDays)
{
inches = Double.parseDouble(dataFile.readLine());
total = total + inches;
days++;
}
return total;
}

public static void main(String[] args) throws IOException
// Main is where execution starts. It opens the data file,
// reads the number of days of rain to be totaled, calls an
// auxiliary method to read and sum the rain fall, and prints
// the average rain fall on the screen
{
double totalRain;
int numDays;
double average;
// Instantiate and open the data file
dataFile = new BufferedReader(new FileReader("rainFile.in"));
// Input the number of days
numDays = Integer.parseInt(dataFile.readLine());

totalRain = getInches(dataFile, numDays);

if (numDays == 0)
System.out.println("Average cannot be computed for 0 days.");
else
{
average = totalRain / numDays;
System.out.println("The average rainfall over " +
numDays + " days is " + average);
}
dataFile.close();
}
}



For some reason that gives me four errors, and I'm not sure what I did wrong.

This is what I am trying to figure out, but I can't find the problem:

How many inches of rain there was...

compared to...




/* Class RainFall calculates the average rainfall over a period
of days. The number of days and the rain statistics are in
file Rain.in */

import java.io.*;public class Rain2
{
// Declare dataFile for input
private static BufferedReader dataFile; static double getInches(BufferedReader dataFile,
int numberDays) throws IOException // Reads and returns the total inches of rain
{
double total = 0.0;double inches;int days = 1; while (days <= numberDays)
{inches = Double.parseDouble(dataFile.readLine());total = total + inches;
days++;} return total;
}public static void main(String[] args) throws IOException
// Main is where execution starts. It opens the data file,
// reads the number of days of rain to be totaled, calls an
// auxiliary method to read and sum the rain fall, and prints
// the average rain fall on the screen
{
double totalRain;int numDays; double average;
// Instantiate and open the data file
dataFile = new BufferedReader(new FileReader("rainFile.in"));
// Input the number of days
numDays = Integer.parseInt(dataFile.readLine());totalRain = getInches(dataFile, numDays); if (numDays == 0) System.out.println("Average cannot be computed for 0 days.");
else
{average = totalRain / numDays;System.out.println("The average rainfall over " + numDays + " days is " + average); } dataFile.close(); }}



How many inches of rain there was in the above program ^

...

I have no idea what happened, and I want to know those answers.

>.<

*Sighs*


EDIT:

I got the SAME errors for both:

Exception in thread "main" java.io.FileNotFoundException: rainFile.in (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at RainFall.main(RainFall.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

snoop
28th January 2007, 07:14 PM
Uh that's kinda obvious if you read the error... the file doesn't exist.. Place it in the same directory as your class file.

Zeta-kun
30th January 2007, 08:00 PM
you should add a if(fileexist()) and/or add a try-catch clause