try-catch - Variablen werden nicht an return übergeben

Sherry

Aktives Mitglied
Hallo,

ich habe hier Anfängerfragen zu den Exceptions. Unten seht ihr eine Main-Klasse und eine Parser-Klasse. Sinn und Zweck ist es, das Parser dei Google-Api mit zwei Orten abfragt und dann die Entfernung und Zeitdauer zurück gibt.
Die Ausgabe ist aber bislang nur

Von Stuttgart nach München:
Dauer : 0,00 h
Entfernung: 0,00 km



Parser.java:
Java:
/**
package com.javamaker.distancecalculator;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Parser {
	private String start;
	private String destination;
	private double duration;
	private double distance;
	private String myUrl;
	
public double parseduration(String start, String destination) throws SAXException, IOException, XPathExpressionException, ParserConfigurationException {
		try {
			URL myUrl = new URL("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + start + "&destinations=" + destination + "&mode=driving&language=de-DE&sensor=false");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(myUrl.openStream());
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expression = xpath.compile("/DistanceMatrixResponse/row/element/distance/value");
			Double duration = (Double.valueOf(expression.evaluate(document, XPathConstants.STRING).toString()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return duration;
	}
	
	public double parsedistance(String start, String destination) throws SAXException, IOException, XPathExpressionException {
		try {
			URL myUrl = new URL("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + start + "&destinations=" + destination + "&mode=driving&language=de-DE&sensor=false");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(myUrl.openStream());
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expression = xpath.compile("/DistanceMatrixResponse/row/element/duration/value");
			Double distance = (Double.valueOf(expression.evaluate(document, XPathConstants.STRING).toString()))/1000;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return distance;
	}


	public void setStart(String start) {
		this.start = start;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public double getDuration() {
		return duration;
	}

	public double getDistance() {
		return distance;
	}

}

Main.java:
Java:
**
package com.javamaker.distancecalculator;

import java.io.IOException;

import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws XPathExpressionException 
	 */
	public static void main(String[] args) throws XPathExpressionException, SAXException, IOException {
		String start = args[0];
		String end = args[1];
		Parser parseUrl = new Parser();
		parseUrl.setStart(start);
		parseUrl.setDestination(end);
		System.out.println("Von " + start + " nach " + end + ":");
		System.out.printf("Dauer     :     ");
		System.out.printf("%.2f", parseUrl.parseduration(start, end));
		System.out.println(" h");
		System.out.printf("Entfernung:     ");
		System.out.printf("%.2f", parseUrl.parsedistance(start, end));
		System.out.println(" km");
	}

}

Irgendwie klappt die der Variablen duration und distance aus den try-catch-Blocks nicht an den return der Methode.
Könnt ihr mir bitte zeigen, was ich falsch mache bzw. mein Denkfehler ist.

Grüße
Sherry
 
Zuletzt bearbeitet:
Wie du schon geschrieben hast, ist die Variable lokal nur innerhalb des try-Blocks vorhanden.
Damit es auch außerhalb verwendbar ist, musst du es so schreiben:

Java:
	public double parseduration(String start, String destination) throws SAXException, IOException, XPathExpressionException, ParserConfigurationException {
		double duration = -1;
		try {
			URL myUrl = new URL("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + start + "&destinations=" + destination
					+ "&mode=driving&language=de-DE&sensor=false");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(myUrl.openStream());
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expression = xpath.compile("/DistanceMatrixResponse/row/element/distance/value");
			duration = (Double.valueOf(expression.evaluate(document, XPathConstants.STRING).toString()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return duration;
	}
 

Zurück
Oben