2

I am creating desktop application and I am facing a problem that I can't find a way to check if my is variable empty or not. In field user must enter a number. OK - if he enters number so how to check that he really entered it and did not left empty field? Should I try to convert my variable amziusCheck to String and then I could use .length(), am I right? Or there is another way?

IntStream amziusCheck = programuotojoAmzius.getText().chars();
HenrikasB
  • 321
  • 1
  • 9
  • `String.isEmpty()` is even better. I suggest to first use `trim` to prevent `" "` to return `false`. So `text.trim().isEmpty()` – AxelH Jan 08 '19 at 12:13
  • Ahm.. You got me wrong probably. Right now my variable is int type not String, it was just a idea to make it to String – HenrikasB Jan 08 '19 at 12:18
  • Well, you mentions `String,length` so I followed you direction. What is the type of `programuotojoAmziu` exactly ? An [mcve] would be better to give a solution based on your current situation – AxelH Jan 08 '19 at 12:19
  • @AxelH this is my fx:ID. I want this 'programuotojoAmzius' use as int. By the way I want to check is this variable empty or not after launching program. – HenrikasB Jan 08 '19 at 12:22

2 Answers2

2

You could extract the text (programuotojoAmzius.getText()) first, check that the text is not empty, and then process the IntStream if the text is not empty:

String value = programuotojoAmzius.getText();

if (!value.isEmpty()) {
    IntStream intStream = value.chars();
    // ...process intStream...
}

Note that as of JDK 11, you can also check that a String is blank (either empty or contains only whitespace) using the String::isBlank:

String value = programuotojoAmzius.getText();

if (!value.isBlank()) {
    IntStream intStream = value.chars();
    // ...process intStream...
}
Justin Albano
  • 3,809
  • 2
  • 24
  • 51
1

If you want to check if a String is empty or not, use String.isEmpty() it returns a boolean, true if it is empty, false, otherwise.

Daniel B.
  • 2,491
  • 2
  • 12
  • 23