Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Java] Why the method shortest has for (int column = 0; column < phrase[0].lengt

ID: 3582156 • Letter: #

Question

[Java]

Why the method shortest has for (int column = 0; column < phrase[0].length-1; column++)

while the method howMany has for(int column = 0; column < phrase[0].length; column++)?

I mean, shouldn't it be for(int column = 1; column < phrase[0].length; column++) or for(int column = 0; column < phrase[0].length - 1; column++)?

But the answer is for(int column = 0; column < phrase[0].length; column++).

public String shortest()
    {
        String shortest = phrase[0][0];
        for (int row = 0; row < phrase.length; row ++)
        {
            for (int column = 0; column < phrase[0].length-1; column++)
            {
                if(phrase[row][column].length() < shortest.length())
                {
                    shortest = phrase[row][column];
                }
            }
        }
        return shortest;
    }

public int howMany(char letter)
    {
        int appearance = 0;
        for(int row = 0; row < phrase.length; row++)
        {
            for(int column = 0; column < phrase[0].length; column++)
            {What
                String word = phrase[row][column];
                word = word.toLowerCase();
                for(int i = 0; i < word.length(); i++)
                {
                    if(word.charAt(i) == letter)
                    {
                        appearance++;
                    }
                }
            }
        }
        return appearance;
    }

Explanation / Answer

Yes, thats right. the method should be defined liek this,

public String shortest()
    {
        String shortest = phrase[0][0];
        for (int row = 0; row < phrase.length; row ++)
        {
            for (int column = 0; column < phrase[0].length; column++)
            {
                if(phrase[row][column].length() < shortest.length())
                {
                    shortest = phrase[row][column];
                }
            }
        }
        return shortest;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote