-4
public static String split (String vector){
    String vector1 = "";
    String vector2 = "";
    final int SIZE = vector.length();
    int firsthalf = 0;
    int secondhalf = SIZE/2;
    while (firsthalf< SIZE/2){
        vector1+= vector.charAt(firsthalf);
        firsthalf++;
    }
    while (secondhalf< SIZE){
        vector2+= vector.charAt(secondhalf);
        firsthalf++;
    }
    return vector1;
    return vector2;

} 

Hi so i know that this question might have been asked frequently by java noobs like myself but i dont understand why vector2 is out of reach as java error report calls it, and what can i do to return the vector2

Reddevil
  • 125
  • 3
  • 12
  • Two things : - a function returns a single value, no matter what. You can return "several" results by calling methods of the input arguments, which is called side-effects (tu populate a collection for example) or by wrapping them in a single object - when `return` is found, any code following this return **in the same scope** will be ignored. It is called *dead code* and causes a compilation failure. – Dici Apr 19 '15 at 13:55

1 Answers1

0

A method can only have a single return value....your first return is vector1 ...hence vector2 is out of reach

consider this...

String result = split("TestVector");

using your method what is the value of result...

YOu could consider returning a List or Array of Strings.

Narrim
  • 567
  • 8
  • 22
  • then how else shoud i do to return two values? – Reddevil Apr 19 '15 at 13:56
  • Your question is a duplicate my answer covers some of what you need but look at http://stackoverflow.com/questions/2832472/how-to-return-2-values-from-a-java-function – Narrim Apr 19 '15 at 13:58