1

I have a TextField within a GridPane that I simply want to have it's user-typed contents read into an Integer that I will then use for multiple (non-double) calculations.

TextField userInput = new TextField(); userInput.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> { if (!newValue.matches("\\d*")) { userInput.setText(newValue.replaceAll("[^\\d]", "")); } }); sortButton.setOnMousePressed( e -> { int savedValue = Integer.parseInt(userInput); int outputMath = savedValue * 3; int outputExpo = savedValue * 10; int outputQuad = savedValue * 4; }); 

The listener does a great job of making sure nothing but numbers gets accepted, but those numbers are read as a string that I don't seem able to use for calculations.

The problem child is that the Integer.parseInt(userInput) just doesn't seem to do the trick here. Any advice?

1
  • but you could try other alternatives, like Integer.valueOf() Commented Apr 1, 2016 at 16:14

1 Answer 1

3

On this line:

int savedValue = Integer.parseInt(userInput); 

what you are actually doing is to try to parse the variable userInput which is a TextField.

You should parse the content of the textProperty of the TextField instead:

int savedValue = Integer.parseInt(userInput.getText()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.