46.1k views
0 votes
Implement the method countSecondInitial which accepts as parameters an ArrayList of Strings and a letter, stored in a String). (Precondition: the

String letter has only one character. You do not need to check for this.) The method should return the number of Strings in the input ArrayList
that is in the second index with the given letter. Your implementation should ignore the case of the Strings in the ArrayList
Sample Run:
Please enter words, enter STOP to stop the loop.
find
dice
hi
dye
STOP
Enter initials to search for, enter STOP to stop the loop.
1
Search for 1: 3
Hint - the algorithm to implement this method is just a modified version of the linear search algorithm.
Use the runner class to test your method but do not add a main method to your U7_14_Activity One.java file or your code will not be scored
correctly.
We have provided a couple of sample runs that can be used to test your code, but you do not need to include a Scanner and ask for user input when
submitting your code for grading because the grader will test these inputs automatically.

User Brovar
by
7.4k points

1 Answer

3 votes

Answer:

import java.util.ArrayList;

public class U7_L4_Activity_One

{

public static int countSecondInitial(ArrayList<String> list, String letter)

{

char letter1 = letter.toLowerCase().charAt(0);

int count = 0;

String phrase = "";

for(int i = 0; i < list.size(); i++)

{

phrase = list.get(i).toLowerCase();

if(phrase.charAt(1) == letter1)

{

count++;

}

}

return count;

}

}

Step-by-step explanation:

I genuinely couldn't tell you why the program cares, but it worked just fine when I ran the search word as a char variable as opposed to a 1-length string variable, i.e. the inputted 'letter' parameter.

If it works, it works.

PS. You got 33% RIGHT because you didn't have a 'public static void main' as the method header. I don't doubt your code works fine, but the grader is strict about meeting its criteria, so even if your code completed it perfectly and THEN some, you'd still get the remaining 66% wrong for not using a char variable.

User Xyz
by
7.4k points