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

Modify the algorithm in this section to use an array of strings, and to find the

ID: 3574879 • Letter: M

Question

Modify the algorithm in this section to use an array of strings, and to find the last match. In this example, a match is a word of a specified length. For example, when asked for the last word of length 3, you should locate “was” at index 7.

Complete the following file:

FindLast.java

import java.util.Scanner;

public class FindLast
{
public static void main(String[] args)
{
String[] words = { "Mary", "had", "a", "little", "lamb",
"it's", "fleece", "was", "white", "as",
"snow" };
  
Scanner in = new Scanner(System.in);
System.out.print("Word length: ");
int wordLength = in.nextInt();

boolean found = . . .;
int pos = . . .;
while (. . .)
{
if (. . .)
{
. . .;
}
else
{
. . .;
}
}

if (. . .)
{
System.out.println("Found " + words[pos] + " at position " + pos);
}
else
{
System.out.println("No word of length " + wordLength);
}
}
}

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class FindLast
{
public static void main(String[] args)
{
String[] words = { "Mary", "had", "a", "little", "lamb",
"it's", "fleece", "was", "white", "as",
"snow" };
  
Scanner in = new Scanner(System.in);
System.out.print("Word length: ");
int wordLength = in.nextInt();
boolean found = false;
int pos = 0, i = 0;
while (i < words.length)
{
if (words[i].length() == wordLength)
{
found = true;
pos = i;
}
i++;
}
if (found)
{
System.out.println("Found " + words[pos] + " at position " + pos);
}
else
{
System.out.println("No word of length " + wordLength);
}
}
}

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