Search code examples
javaarrayselementchars

Given a array with a blank element how do I print all elements except the blank one?


I have a array of chars called votacoes. And when I do:

for (int i = 0; i < nDeputados; i++) {
    System.out.println(votacoes[i]);
}

the output is:

S
A

S
S
S
N
A

As you can see you there is a blank char in index 2.

To print everything except the blank char what is the condition in the following if?

for (int i = 0; i < nDeputados; i++) {
    if(???????????){
        System.out.println(votacoes[i]);
    }
}

Solution

  • for (int i = 0; i < nDeputados; i++)
    {
     if(Character.isAlphabetic(votacoes[i]))
    
          System.out.println(votacoes[i]);
    }
    

    The Character.isAlphabetic() method returns true if the value passed is an alphabet. Hope this helps :)