mercredi 18 mars 2015

JAVA: Call same method with if-cond, without execution of further(old) code

I think what I'm trying to do is a callback.


I have a prompt asking the user if he wants to change the wolf. If the user presses "YES" the user may enter two digits. If these don't match my pattern I want to call the method again. So far, so good.


But after I entered a wrong pattern, the prompt appears again, and then after I enter a right pattern, I still get an error, this time it passed the wrong pattern from before, to the try catch of parse.


This is the point I don't get. On failed pattern I called the method again, so the code should exit my method, not store any value and reenter it again, right? So why is it trying to parse the wrong pattern from the method call before.


It should try to parse the newly entered value (the right pattern) but stucks with using the old value.


I would apprecciate some help of you guys. Thanks in advance.



public World changeWolf(Wolf w) {
out("Change the wolf? (Yes/No)");

switch (scannerInstance.nextLine()) {
case "NO":
break;
case "YES":
changeCrds(w);
changeWolf(w);
break;
default:
out("Invalid.");
changeWorld(w);
break;
}
return w;
}


public void changeCrds(Wolf w){
String input = getNext();
String[] inputField;

if (!input.matches("\\d+,\\d+")) {
out("Invalid. REGEX error");
changeCrds(w);
}

inputF = input.split(",");

int w = 0; int t = 0;

try {
w = Integer.parseInt(inputF[0]);
t = Integer.parseInt(inputF[1]);
} catch (Exception e) {
out("Invalid. PARSe error");
changeCrds(w);
}
}

Aucun commentaire:

Enregistrer un commentaire