• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Exceptions problem

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
prerna what is the exception? Can you please post the exact error message you get?

Layne
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic