Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions problem

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prerna what is the exception? Can you please post the exact error message you get?

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic