-3

I have to do some kind of Google Maps like program on Java, I have the following code:

public double CGlon(double graus, double min, double sec)
    {
        double min2;
        graus =(int) longitud;
        min = (longitud - graus)*60;
        min2 = (int) min;
        sec = (min-min2)*60;
        return(graus);
        return(min); //Unreachable point
        return(sec); //Error
    }

where I have to convert the coordinates, 41,234234º into 41º 23' 122" , for example. And I'm calling on the main program:

public class GPS {

private static final int graus = 0;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    TempClass lloc1 = new TempClass();
    TempClass lloc2 = new TempClass();
    TempClass lloc3 = new TempClass();
    TempClass lloc4 = new TempClass();

//Inicialitzacio de les dades
    //ETSE
    lloc1.nomSet("ETSE");
    lloc1.latitudSet(41.1258048);
    lloc1.longitudSet(1.2385834);

System.out.println(lloc1.CGlat(graus, 0, 0)+" graus"+lloc1.CGlat(0, 0, 0)+" minuts y "+lloc1.CGlat(0, 0, 0)+" segons de latitud.");

the problem is that i'm having an unreachable error on the 'min' variable and i dont know why.

  • 3
    You can return once, if you want to return as per some condition then pass it in some `if` or etc. – user3145373 ツ Sep 29 '14 at 15:02
  • First, that isn't legal Java code, **return** is a keyword not a method. Second, what happens when you return a value from a method? What line does the JVM want to execute next? – azurefrog Sep 29 '14 at 15:02

4 Answers4

2

The first return statement stops execution of the method, so the second and third return statements cannot be reached. If you're trying to return three values, return an array:

public double[] CGlon(double graus, double min, double sec)
{
    double min2;
    graus =(int) longitud;
    min = (longitud - graus)*60;
    min2 = (int) min;
    sec = (min-min2)*60;
    return new double[] { graus, min, sec };
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @manutheking - It looks like you're printing the array object itself. This will print the identity hash of the array, not it's values. Try printing the relevant element(s) using subscripts or else use `Arrays.toString(CGlon(...))` to print the entire array as a readable string. – Ted Hopp Sep 29 '14 at 16:17
  • after do this it shows no error but after compiling and running i get this on the console: `[D@79b0edb2 º[D@4ec57f88 '[D@837e21f ''
    ` [D@cde6570 graus de longitud.' `
    – manutheking Sep 29 '14 at 16:17
  • @manutheking - Um, didn't you just post that comment and then delete it? See my comment just above. – Ted Hopp Sep 29 '14 at 16:19
  • yes, I'm sorry I just messed up with the comment. It works fine now but it displays the 3 variables in a row. Is there any thing I can do to just show 1 of the variables? – manutheking Sep 29 '14 at 16:52
  • @manutheking - Sure. If you want to display, for instance, the second element (`min`), you can print the element at index 0; for instance: `System.out.println("min=" + CGlon(0, 0, 0)[1]);`. – Ted Hopp Sep 29 '14 at 17:34
1

A function (or method) can only return once. How do you expect this to work?:

return(graus);
return(min); //Unreachable point
return(sec); //Error

As soon as the first return is executed, the function has ended. It has resulted in a value and is done executing. So nothing after a return statement will ever be executed.

If you want to return three separate values, create a data structure of some kind which holds those three values together and return an instance of that structure. It might look something like:

public class SomeDataStructure {
    private final double graus;
    private final double min;
    private final double sec;

    public SomeDataStructure(double graus, double min, double sec) {
        this.graus = graus;
        this.min = min;
        this.sec = sec;
    }

    // also create getters for the values
}

Then in your code:

SomeDataStructure result = new SomeDataStructure(graus, min, sec);
return result;
David
  • 208,112
  • 36
  • 198
  • 279
  • @manutheking: As you learn, be aware of the pros and cons of the different solutions offered in the different answers here. Returning an array of values requires less code and is *initially* simpler, but creates no semantic link between the values themselves. In a small case, it's fine. As code increases in complexity, strongly-typed objects present more semantic clarity of the logic of the system, as well as a single place to update logic. There's even an explicit refactoring pattern to move from an array to an object: http://refactoring.com/catalog/replaceArrayWithObject.html – David Sep 29 '14 at 15:13
0

What do you expect that code to do? Why do you have 3 return statements?

Once you return a value, the method does not continue executing. You have other statements after a return value, which is by definition invalid, hence the error.

It seems like you're trying to return more than one value, which isn't possible. Instead, wrap the 3 return values into an Object and return that instead.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

Look at your code:

return(graus);
return(min);
return(sec);

When the method is executed, it returns a value to the caller when it encounters the first return statement. There is no way for the method to return any other value. Consider the calling code

double result = lloc1.CGlat(graus, 0, 0);

What value would result be assigned in your case? The first, second, or third return value? Therefore, your code does not compile.

Now, if you want to return three values from a method, what you really want to do is return a compound object. To that end, create a suitable class to hold your values:

return new Location(graus, min, sec);

where the Location class assigns these to fields that you can query from the calling code, e.g. like so:

Location result = lloc1.CGlat(graus, 0, 0);
System.out.println(result.getGraus() + "°" + result.getMinutes() + "'" + result.getSeconds() + "\"");
Simon Fischer
  • 1,154
  • 6
  • 22