I am trying to write some code that takes the input from a textArea once enter was pressed, then returns it
My problem is, that when the getInput() method is called it only checks once if enter is pressed, then returns null if its not
here is my code:
static String getInput() {
UI.textArea.setOnKeyPressed(ke -> {
switch (ke.getCode()) {
case KeyCode.ENTER:
System.out.println("-UserInput: Pressed Enter");
UI.preInput = UI.textArea.getText();
UI.lines = UI.preInput.split("n");
if (UI.lines.length >= 1) {
input = UI.lines[UI.lines.length - 1];
System.out.println("-UserInput: " + input);
}
break;
case KeyCode.ALT_GRAPH:
System.out.println("-UserALTGR:nInput: " + input + "nPathUser: " + userPath + "nsignedIn: " + signedIn + "naccountNumber: " + accountNumber);
getInput();
break;
}
});
return input;
}
public static void signUpOrLogin() {
say("Choose an option:n[1] Sign upn[2] Log in");
switch (getInput()) {
case "1":
signUpUs();
case "2":
logInUs();
case null:
System.out.println("null");
default:
say("Invalid choice");
}
}
i also tried using a while loop, but the program crashes when doing so:
static String getInput() {
submitted = false;
do {
UI.textArea.setOnKeyPressed(ke -> {
switch (ke.getCode()) {
case KeyCode.ENTER:
System.out.println("-UserInput: Pressed Enter");
UI.preInput = UI.textArea.getText();
UI.lines = UI.preInput.split("n");
if (UI.lines.length >= 1) {
input = UI.lines[UI.lines.length - 1];
System.out.println("-UserInput: " + input);
submitted = true;
}
break;
case KeyCode.ALT_GRAPH:
System.out.println("-UserALTGR:nInput: " + input + "nPathUser: " + userPath + "nsignedIn: " + signedIn + "naccountNumber: " + accountNumber);
getInput();
break;
}
});
} while (!submitted);
return input;
}
public static void signUpOrLogin() {
say("Choose an option:n[1] Sign upn[2] Log in");
switch (getInput()) {
case "1":
signUpUs();
case "2":
logInUs();
case null:
System.out.println("null");
default:
say("Invalid choice");
}
}
New contributor
quokkah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.