Requirements: Modify the code in the WordArray.java file to be dynamic. Instead
ID: 3792708 • Letter: R
Question
Requirements: Modify the code in the WordArray.java file to be dynamic. Instead of searching for the same word each time, make the search word dynamic. (So instead of hard coding a specific word, it will become a variable.) Also allow the user to enter their own list of words to populate into the array. So each time the program runs, the user will have the functionality to search for a different word, in a different list of words in the array. The number of words entered into the array should also be dynamic. The program should also produce a count of the number of instances of the word found in the array.
package JH_samples;
/*
Returns true if an array of Strings contains the String of
"Seattle"; false otherwise.
*/
//import java.util.Scanner;
import javax.swing.JOptionPane;
// import java.util.Scanner;
public class WordArray
{
public static void main(String [] args)
{
// Description of the program
JOptionPane.showMessageDialog( null, " This program calls a method to search an array for the word Seattle");
// Array Declaration
int t = 0;
// Instantiate the Array
// Assign values to the Array
String [] wordList = { "Seattle", "Tacoma", "Bellevue", "Spokane", "Seattle", "Bremerton", "Bellingham" };
for ( int i = 0; i < wordList.length; i++ )
{
// For testing
System.out.println(wordList[i]);
if (wordList[i].contains("Seattle"))
{
t=t+1;
JOptionPane.showMessageDialog(null, "Array value is True:" + wordList[i]);
}
}
if (t > 0)
{
JOptionPane.showMessageDialog(null, "Seattle exists in the Array");
} // end of if
else
{
JOptionPane.showMessageDialog(null, "Seattle Does not exist Exists in the Array");
}
}
}
Explanation / Answer
Hi
i have updated the code and highlighted the code chages below
WordArray.java
//import java.util.Scanner;
import javax.swing.JOptionPane;
// import java.util.Scanner;
public class WordArray
{
public static void main(String [] args)
{
// Description of the progra
JOptionPane.showMessageDialog( null, " This program calls a method to search an array for the word Seattle");
int size = Integer.parseInt(JOptionPane.showInputDialog("Enter the size of an array: "));
String [] wordList = new String[size];
// Array Declaration
int t = 0;
// Instantiate the Array
// Assign values to the Array
//String [] wordList = { "Seattle", "Tacoma", "Bellevue", "Spokane", "Seattle", "Bremerton", "Bellingham" };
for ( int i = 0; i < wordList.length; i++ ){
wordList[i]=JOptionPane.showInputDialog("Enter the string "+(i+1)+": ");
}
String searchString = JOptionPane.showInputDialog("Enter the search string: ");
for ( int i = 0; i < wordList.length; i++ )
{
// For testing
System.out.println(wordList[i]);
if (wordList[i].contains(searchString))
{
t=t+1;
JOptionPane.showMessageDialog(null, "Array value is True:" + wordList[i]);
}
}
if (t > 0)
{
JOptionPane.showMessageDialog(null, searchString+" exists in the Array");
} // end of if
else
{
JOptionPane.showMessageDialog(null, searchString+" Does not exist Exists in the Array");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.