17

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?

5
  • 1
    What's that you are trying to check ? You check if the string is empty and then do a print, and the result as you say is something like "set" +... since you can't see an empty string :). Probably you want to test the not condition: i.e if(!str[i].equals("")) .. Commented Dec 24, 2009 at 6:19
  • Really, simple check if() condition ... Commented Dec 24, 2009 at 6:50
  • Do you think you could edit your question to make it more understandable? From the comments and answers, I can see I'm not the only one that is not sure what you really want... :-( Commented Dec 24, 2009 at 7:07
  • Sorry for typo mistake. Actually that was as you hav mentioned the not condition. Commented Dec 24, 2009 at 7:34
  • not resolving the problem, but it usually preferred to write if("".equals(str[i])) to avoid NullPointerExceptions Commented Dec 24, 2009 at 8:05

6 Answers 6

27

You could try :

if (str[i] == null || str[i].trim().equals("")){
// your code
}
Sign up to request clarification or add additional context in comments.

Comments

15

You can use the Apache Commons Lang to check a String:

if (StringUtils.isBlank(str[i]) {
    ...
}

StringUtils.isBlank is checking if the String is null or empty (i.e. if it is equals to "" when all blank characters are removed).

2 Comments

what happens if someone just enters space?
@AshishDonvir StringUtils.isBlank(" ") returns true. If you want this test to return false, you can use StringUtils.isEmpty(...) instead.
1

I would recommend

if (str[i] == null || str[i].trim().length==0){
// your code
}

or Java 6 onward

if (str[i] == null || str[i].trim().isEmpty()){
// your code
}

Instead of using equals() because equals() method is overkill to test for an empty String

Comments

0

The above code only executes if the string is empty, is that the desired behavior? Also declaring str as new right before the loop pretty much guarantees it will be empty.

If you are trying to assign the values from textboxes you will need to have those assignments somewhere before the loop.

Comments

0

I actually tried to run your code snippet and got a NullPointerException on your conditional

str[i].equals("")

str[i] == null worked for me. Are you sure you put the right code in your question. It seems odd to be testing for an empty string of an empty array you just initialized.

Comments

0

A very simple solution can be like this.

if(str[] != null && !str[].equals(""))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.