user2140209
user2140209

Reputation: 17

Java wait for variable to change

I am sitting with a little problem here, and i has been searching for hours for any soultion now, but cant seem to find any, and i hope you can help me.

I have these methods:

public String getInput(){
    //Wait here somehow
    return "Whatever to return";
}

public void keyTrigger(KeyEvent event){
    if(event.getCode().equals(KeyCode.ENTER)){
        String[] getInput = gameLog.getText().split("\n");
            input = getInput[getInput.length - 1]; //Input is a variable in the class
            //Tell the getInput() to continue from where i waited
    }
}

So if anyone can tell me how the make the first method wait for a response from the other method, i woul be very happy, because none i have tried so far has worked

EDIT... Sorry guys, i have missed out on some details.

1st: I am developing a GUI in JavaFX, and the gameLog variable is a textarea, and thats why im splitting the String on linebreaks.

2nd: when i call getInput() i want it to wait for the user to press enter, then get the input variable

Upvotes: 0

Views: 3290

Answers (3)

James_D
James_D

Reputation: 209330

Instead of trying to block until the variable changes, you should use a StringProperty and register listeners with it.

Here's some skeleton code:

private final StringProperty input = new SimpleStringProperty();

public StringProperty inputProperty() {
    return input ;
}

public final String getInput() {
    return inputProperty().get();
}

public final void setInput(String input) {
    inputProperty().set(input);
}

// event handler (I'm assuming):
public void keyTrigger(KeyEvent event){
    if(event.getCode().equals(KeyCode.ENTER)){
        String[] getInput = gameLog.getText().split("\n");
            inputProperty().set(getInput[getInput.length - 1]); 
    }
}

Now you can have code execute when the input is changed:

inputProperty().addListener((obs, oldInput, newInput) -> {
    // newInput contains the new value set to the input property....
});

The Properties and Bindings tutorial has more details on using JavaFX properties.

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200148

Your immediate request would be satisfied by a CompletableFuture:

public String getInput(){
  final CompletableFuture<String> fut = new CompletableFuture<>();
  commonFuture = fut;
  return fut.join();
}

In the above, you need to provide a variable commonFuture which is accessible both from the above code and from your KeyEvent listener, and will serve as the point of contact between these two pieces of code. In the listener you would say

commonFuture.complete(getInput[getInput.length - 1]);

and at that point the join call in getInput() would complete, returning this value.

However, I urge you to seriously think through your current design, which demands such synchronous blocking. You may be able to rework so that getInput is replaced by a callback method which gets invoked when the input value is available.

Upvotes: 1

Lolso
Lolso

Reputation: 1

I don't really understand your issue here, from common logic I guess your getInput() should read from command line and return the value as String. You don't need to write any special code for it, for reading from command line you should use BufferedReader, see the example below.

public String getInput(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
try{
    line = reader.readLine();
    }catch(IOException e){
        e.printStackTrace();
        }
return line;
}

Also this part of your code, input = getInput[getInput.length - 1]; I don't see a point of getInput.length - 1 because getInput() will return a whole string so you don't need to trim it unless you explicitly want to do it. So you can just write input = getInput();, with assumption that variable input is of type String.

Upvotes: 0

Related Questions