Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help for trapezoidal rule java code

Status
Not open for further replies.

georgemaravich

Programmer
Dec 3, 2014
2
Hi guys. Need help...
I tried running the code for trapezoidal rule. It's my project in Numerical Methods, here's the 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!

 
Hi

Really missing the [tt]class[/tt] declaration line or just a copy-pasting mistake ?


Feherke.
feherke.ga
 
Copy paste mistake dude. Any thoughts in how to get rid with FileNotFoundException error? Still can't find answers until now...
 
Hi

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

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top