aspose file tools*
The moose likes Java in General and the fly likes Exceptions problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exceptions problem" Watch "Exceptions problem" New topic
Author

Exceptions problem

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi

I have the following program I get an error sayin Exception in "Main" .
Can anybody tell me where am I wrong.





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.valueOf(dataFile.readLine()).doubleValue();
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.valueOf(dataFile.readLine()).intValue();

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();
}
}
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Rather than simply throwing the exception from main, why not catch it and print it out? Do something like this:



That should give you more information to work with.


SCJP Tipline, etc.
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
hi
I guess theres an exception occuring in the main function and since you are just throwing it but not catching it, your code is breaking somewhere and thus the exception is thrown. You have to catch it using the try catch block.


"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Here's what I get when I compile and run your program. What do you get?

C:\_Work\java>java RainFall
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:34)

p.s. P*L*E*A*S*E use code tags next time (button labeled "CODE" next to the smilies).
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
prerna what is the exception? Can you please post the exact error message you get?

Layne


Java API Documentation
The Java Tutorial
 
 
subject: Exceptions problem
 
Similar Threads
Passing parameters to functions help.
ArrayList Exceptions
why is my calstandardDev() returning 0?
Drawing in a loop
code help