Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Warum Exeption? (http://www.informatikerboard.de/board/thread.php?threadid=228)


Geschrieben von mausi am 04.07.2007 um 23:34:

  Warum Exeption?

Hallo!

Ich haben diesen Code zur Trapezregel. Nun bekomme ich eine Expetion und ich weiß nich warum?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0


public class TrapezRegel {


static double f(double x) {
return Math.exp(- x * x / 2) / Math.sqrt(2 * Math.PI);
}


static double integrate(double a, double b, int N) {
double h = (b - a) / N; // step size
double sum = 0.5 * (f(a) + f(b)); // area
for (int i = 1; i < N; i++) {
double x = a + h * i;
sum = sum + f(x);
}

return sum * h;
}



public static void main(String[] args) {
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
System.out.println(integrate(a, b, 1000));
}

}

Ich habe den code so bekommen und er sollte laufen. Ich wollte ihn bunuzten um es für andere Funktionen zu testen. Mit meinem Wissen komme ich aber nicht drauf warum nun die Exeption geschmissen wird. Nun weiß ich nich wie ich das korrigieren kann...

Kann mir hier jemand helfen bitte?



Geschrieben von Tobias am 05.07.2007 um 15:40:

 

Kann es sein, dass die Exception nur kommt, wenn keine Argumente (also zwei Doublezahlen) dem Programm übergeben werden?

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
   public static void main(String[] args) {
            
            if (args.length < 2) {
                System.out.println("a und b angeben.");
                return;
            }
            
            try {
                double a = Double.parseDouble(args[0]);
                double b = Double.parseDouble(args[1]);
                System.out.println(integrate(a, b, 1000));
                
            } catch (NumberFormatException e) {
                System.out.println("Eingegebene Parameter sind keine Double-Werte.");
            }
   }


Forensoftware: Burning Board, entwickelt von WoltLab GmbH