0

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.

1 Answer 1

4

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"));
    }
}
Sign up to request clarification or add additional context in comments.

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.