INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help for trapezoidal rule java code

Need help for trapezoidal rule java code

(OP)
Hi guys. Need help...
I tried running the code for trapezoidal rule. It's my project in Numerical Methods, here's the code:

<

CODE --> Java

static double trapezoidRule (int size, double[] x, double[] y)
   {  double sum = 0.0,
             increment;

      for ( int k = 1; k < size; k++ )
      {//Trapezoid rule:  1/2 h * (f0 + f1)
         increment = 0.5 * (x[k]-x[k-1]) * (y[k]+y[k-1]);
         sum += increment;
      }
      return sum;
   }

   public static void main ( String[] args ) throws Exception
   {  String   fileName = args.length > 0 ? args[0] : "InpData.txt";
      Scanner  inp = new Scanner(new File(fileName));
      int      k, size;
      double[] x, y;
      double   integral;

      size = inp.nextInt();
      System.out.println ("Number of points:  " + size);

      x = new double[size];
      y = new double[size];

      for ( k = 0; k < size; k++ )
      {  x[k] = inp.nextDouble();
         y[k] = inp.nextDouble();
      }
      integral = trapezoidRule (size, x, y);
      System.out.printf ("Integral:  %4.4f\n", integral);
      System.out.printf ("Check:  log(%2.2f) = %8.8f\n",
                         x[size-1], Math.log(x[size-1]) );
   }
} 


It cannot be compiled and I always get FileNotFoundException. I found on javadocs that this will be thrown when a file with the path name does not exist. Please help. Thanks!

RE: Need help for trapezoidal rule java code

Hi

Really missing the class declaration line or just a copy-pasting mistake ?

Feherke.
feherke.ga

RE: Need help for trapezoidal rule java code

(OP)
Copy paste mistake dude. Any thoughts in how to get rid with FileNotFoundException error? Still can't find answers until now...

RE: Need help for trapezoidal rule java code

Hi

  • Are you passing it input file name when running it ?
    • If yes, what is that file name ?
  • Does the input file ( with the name you pass in parameter or the default InpData.txt ) exist ?

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close