jeudi 15 janvier 2015

Variable inside if used outside [duplicate]


This question already has an answer here:




What i'm trying to to is to get the first char of string a, and last char of string b. Empty a should return "@+lastb" and empty b should return "firsta+@".


examples: a = "hello", b = "hi" should return "hi"; a = "" and b = "hi" returns "@i";



public String lastChars(String a, String b) {
if(a.length() > 0) {
String firstA = a.substring(0,1);
}
else {
String firstA = "@";
}
if(b.length() > 0) {
String lastB = b.substring(b.length()-1);
}
else {
String lastB = "@";
}
return firstA + lastB;
}


the error message i get is that the variables cannot be resolved, which im guessing means that they never were made?


Aucun commentaire:

Enregistrer un commentaire