[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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.