Question

I have this following code:

public void Print() {
        String formatString = "%12s %7s %9s\n";
        System.out.format(formatString, "Surname", "Initial", "Extension");
        for (int i = 0; i < directory.length - 1; i++) {
            System.out.format(formatString, (Object[]) directory[i].split("\t"));
        }

    }

The point of this code is to make an array which contains something like "Smith i 0472" (the space is actually a tab). This code works perfectly when the size of the Array is the amount of things to be printed but throws an error if the array is for example, 100 and i only have 20 elements in it. I need the array to be this size. Thank you.

Sorry if I failed to make this clear enough.

Was it helpful?

Solution

Can't you just check if the element is null before printing?

for (int i = 0; i < directory.length - 1; i++) {
    if (directory[i] != null) {
        System.out.format(formatString, (Object[]) directory[i].split("\t"));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top