ParseDouble

uccie

Mitglied
Hallo,

ich hab ein kleines Problem. Ich soll 6 Double Werte von einer Zeile einlesen aber funktioniert bei mir leider nicht. Kann jemand mir sagen, wo mein Fehler ist.

Java:
public class TriangleArea {
/** 
 * The program calculates the area of a triangle using the Cartesian coordinates and
 * transforming them to Euclidean distance.
 */
	public static Length length (double ax, double ay, double bx, double by, 
			double cx, double cy){
		
		
		
		double a = Math.sqrt((cx-bx)*(cx-bx) + (cy-by)*(cy-by));
		double b = Math.sqrt((cx-ax)*(cx-ax) + (cy-ay)*(cy-ay));
		double c = Math.sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by));
		
	
		return new Length(a, b, c);
		
		// distance in euclidean space
	}
	public static double cosineTheorem(double a, double b, double c){
		
		double cosGama = (a*a + b*b - c*c)/ (2*a*b);
		double gama = Math.acos(cosGama);
		return gama; 
	}
	
	public static double area(double a, double b, double gama){
		double sinGama = Math.sin(gama);
		double area = (a*b*sinGama)/2;
		
		return area;
		
	}
	
	
	public static void main (String[] args){
		

		
		double ax = Double.parseDouble(args [1]);
		double ay = Double.parseDouble(args [2]);
		double bx = Double.parseDouble(args [3]);
		double by = Double.parseDouble(args [4]);
		double cx = Double.parseDouble(args [5]);
		double cy = Double.parseDouble(args [6]);
		
		
		Length q =  length(ax, ay, bx, by, cx, cy);
        double result = area(q.getA(), q.getB(), cosineTheorem(q.getA(), q.getB(), q.getC()));
		System.out.println(result);
		
		
	}


Java:
public class Length { 
	
	private  double a;
	private  double b;
	private  double c;
	
	
	public Length(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}
	
	public  double getA () {
		return this.a;
	}
	
	public  double getB () {
		return this.b;
	}
	public  double getC () {
		return this.c;
	}
}

Vielen Dank im Voraus
 

eRaaaa

Top Contributor
Was genau funktioniert denn nicht?
Wie rufst du das Programm denn auf ?
Kommt ein Fehler, wenn ja welcher?
(kommt evtl. eine ArrayIndexOutOfBoundsException? dann könnte das an folgendem liegen:
Java:
        double ax = Double.parseDouble(args [1]);
        double ay = Double.parseDouble(args [2]);
        double bx = Double.parseDouble(args [3]);
        double by = Double.parseDouble(args [4]);
        double cx = Double.parseDouble(args [5]);
        double cy = Double.parseDouble(args [6]);

der Index bei einem Array beginnt bei 0)
 

Murray

Top Contributor
Arrays sind null-basiert; die Indizes müssen also 0-5 und nicht 1-6 sein (im Ggs. zu C enthält args[0] nicht etwa den Programmnamen, sondern bereits das erste Argument)

//EDIT: zu spät
 

uccie

Mitglied
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TriangleArea.main(TriangleArea.java:41)

Ich habe 0-5 gemacht, aber es kommt wieder das Selbe raus.
 

eRaaaa

Top Contributor
Dann rufst du anscheinend das Programm falsch auf. Du musst deinem Programm die 6 Zahlen als Argument mitgeben:

$ java TriangleArea zahl1 zahl2 zahl3 .....
 

eRaaaa

Top Contributor
Das probiere ich auch. Aber ich kann bis $ java TriangleArea zahl1 zahl2 zahl3 ..... überhaupt nicht kommen...

Ich glaube wir reden aneinander vorbei..wie startest du momentan dein Programm? In Eclipse? In Netbeans? In einer anderen Entwicklungsumgebung? per Kommandozeile?

Sollte eines der ersten beiden zutreffen, könnte folgendes helfen:

  1. In Eclipse
    kann man Programm-Argumente mit Run → Run
    Configurations... → Arguments ubergeben (also deine Zahlen)
  2. In Netbeans: Ausführen → Projektkonfiguration festlegen → Anpassen →
    Argumente (benutze normal kein Netbeans, kann also auch leichter/anders gehen/sein)

bei letzterem, siehe oben ^^
 

uccie

Mitglied
Hey,

funktioniert schon perfekt. Danke dir :). Ich hab mir gedacht, dass ich die Argumente nur mit RUN übergeben kann (Eclipse).
Super :).
 

Ähnliche Java Themen


Oben