Answer:
hasEmpty = false;
for (int k = 0; k < names.length; k++)
Step-by-step explanation:
The code run successfully from my own end. I just modify the integer declaration for k to be within the for loop initialization even though it is not really much an issue.
Better still you can use names[k].isEmpty() to check if an element is empty.
public class MyClass {
public static void main(String args[]) {
String[] names = new String[]{"John", "doe", "", "James", "Smith"};
boolean hasEmpty = false;
for (int k = 0; k < names.length; k++)
{
if ((names[k] == null) || (names[k].isEmpty()))
//if ((names[k] == null) || (names[k].equals("")))
{
hasEmpty = true;
}
}
System.out.println(hasEmpty);
}
}
Using any of the commented if-statement will work correctly. In the above sample, I got an out of true. My sample array contain one empty element.