If I have a string that contains “Cat”
how could I check before hand if anything exists in each individual charAt case.
Here is the sample code
for (int i = 0; i < array.length; i++)
{
System.out.println();
for (int j = 0; j < size; j++)
{
System.out.print(array[j].charAt(i) + " ");
}
}
Once i reaches 3 you would get an out of bounds exception. Is there a way that it could print an empty space?
EDIT: Sorry I wasn’t clear at all. There are multiple strings that are being printed vertically. So assume the largest string is size of 10 and that the smallest is size of 4. The first four char of each string print fine. But an out of bounds error occurs when the fifth char tries to print since it doesn’t exist. Is there any way around that?
You’ve got
iandjthe wrong way around, that’s all.You should have:
And to make your code more robust, you should rewrite the inner loop to go until it reaches the length of
array[i], instead of a pre-definedsizevariable.Update: If you want to print the array of strings vertically (so each string is in a column), this is what your code should look like: