I don't know what is wrong with the below code.... I am getting input from a textbox and putting the input in a string. If the textbox is empty it will return a empty string. In the below code
String[] str = new String[9];
for(int i=0;i<9;i++){
if(str[i].equals("")){
System.out.println("set " + cmds[i] + " " + str[i]);
WriteThread.sendCommand("set " + cmds[i] + " " + str[i] + "\n", false);
}
}
In the above code str[i]
stores the input text of textboxes and I am trying to check if any element of the array is empty. I also tried with str[i] == ""
and str[i] == null
but no luck. The statement inside the if block if i am printing the string str[i]
, it shows nothing that means it is empty.
Am I doing anything the wrong way?
You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.
Use String. isEmpty(), or StringUtils. isEmpty(String str) if you need a null check.
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.
You could try :
if (str[i] == null || str[i].trim().equals("")){
// your code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With