How to check if a String is null and add in an array?

Asked

Viewed 508 times

0

I have a problem, I have several variables (String) with the following names: textTransferd1, textTransferd2 to 73 (textTransferO73).

At certain times the variable textTransferd2 for example may take an empty value or not, depending on the situation.

I would like to mount an array, but I need to check if the String textTransferd1 to textTransferd73 is empty, but add to the array.

Just follow my code:

// INFORMATION RETRIEVAL

    Bundle extra = getIntent().getExtras();

    String textoTransferido1 = extra.getString("texto1");
    String textoTransferido2 = extra.getString("texto2");
    String textoTransferido3 = extra.getString("texto3");
    String textoTransferido4 = extra.getString("texto4");
    String textoTransferido5 = extra.getString("texto5");
    String textoTransferido6 = extra.getString("texto6");
    String textoTransferido7 = extra.getString("texto7");
    String textoTransferido8 = extra.getString("texto8");

// CREATING THE ARRAY

        final String[] frases =
                {
                        textoTransferido1,
                        textoTransferido2,
                        textoTransferido3,
                        textoTransferido4,
                        textoTransferido5,
                        textoTransferido6,
                        textoTransferido7,
                        textoTransferido8
                }

In the array I don’t know if it was right to create an array that would check each item and if not null then add the item in the array.

Since the array does not allow the item to be null or empty, I have to check the situation and only add the ones that have values.

Thank you.

2 answers

2


You can use a for + list.

Bundle extra = getIntent().getExtras();

List<String> textos = new ArrayList<>();
int numeroDeTextos = 8;

for (int i = 0; i < numeroDeTextos; i++) {
  String texto = extra.getString("texto" + i);

  // Verifica se é nulo ou vázio.
  if (texto == null || texto.trim().length() == 0) {
    continue;
  }

  textos.add(texto);
}

String[] arrayDeTextos = textos.toArray(new String[0]);
  • 1

    You can simplify the code a little by modifying If to if(text != null && text.Trim(). length() > 0) text.add(text);

  • Yeah, I know, I even thought I’d put it this way, but I thought it would be more readable the way I put it, silly thing.

  • it would not be more readable to have the comparison texto.Equals(String.Empty)?

  • What you could do is: "".equals(texto), but it wouldn’t work for texts that only have spaces, which is why I used the texto.trim ...

  • Thank you very much, Leonardo! Solved my problem, thanks for sharing your knowledge. Hug!!!

2

If your Extras are only having such texts, would do so, so the code does not depend on the name of each extra or the amount of them:

Bundle extras = getIntent().getExtras();
if (extras != null) {
   List<String> textos = new ArrayList<>();
   for (String key : extras.keySet()) {
       String value = extras.getString(key);
       if(value != null && value.trim().length() > 0)
          textos.add(value);
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.