Question

write a program which include a class containing an array of words (strings).The program will search...

write a program which include a class containing an array of words
(strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value.

enhancements:

make the array off words dynamic, so that the use is prompter to enter the list of words.

make the word searcher for dynamic, so that a different word can be searched for each run

produce a total count of the number of time the word is found, so that if the word is found more than once in the array, atotal count is porduced

All above are the question:Below are what i typed, can you teach me how to fix it. there are no error but i cannot finish some questions above

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Davidhw1
{
public static void main(String[] args)
{String name;
   // Description of the program
   JOptionPane.showMessageDialog( null, "\n This program calls a method to search an array for the word ");
       //create an array
   String [] list= new String[10];
   Scanner scan= new Scanner(System.in);
for (int x=0;x<10;x++)//create a list
{
   System.out.print("Enter name:");name=scan.nextLine();
   list[x]=name;
}
int t=0;
for(int i=0;i<list.length;i++)
{System.out.print("Enter the word you want to search:");
String x=scan.nextLine();//enter the word you want to search
if(list[i].contains("x"))
{
   t=t+1;
   System.out.println("The word is in the array");
}
}
if (t > 0)
{
System.out.print( "The word exists in the Array");

}
else{
   //not found
   System.out.println("The word is not in the array");

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Modified java program that prompts user to enter
number of string to enter and then prompts the strings
and then prompt for the search string and then prints
the count of the string in the list */
//Davidhw1.java
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Davidhw1
{
   public static void main(String[] args)
   {
       //Create a scanner class object to read input from keyword
       Scanner scan= new Scanner(System.in);  
       String name;
       // Description of the program
       JOptionPane.showMessageDialog( null,
               "\n This program calls a method to search an array for the word ");
       //Declare an array of type string
       String[] list;
       //declare an integer variable,N
       int N;

       System.out.println("Enter number of words to read from keyword");      
       //prompt user to enter size of the array and convert to ineger
       N=Integer.parseInt(scan.nextLine());

       //create array list of dynamic size ,N
       list=new String[N];

       //read names from user
       for (int i=0;i<N;i++)//create a list
       {
           System.out.print("Enter name:");
           name=scan.nextLine();
           list[i]=name;
       }

       System.out.print("Enter the word you want to search:");
       //enter the word you want to search
       String searchName=scan.nextLine();

       //Calling method searchName passing list and searchName as input
       //arguments
       int result=searchName(list,searchName);
       //Check if result is 0
       if (result==0)
       {          
           System.out.println("The word is not in the array");          
       }
       else{
           //othrewise print number of times the search name in the list
           System.out.print( "The word exists in the Array "+result+" times");
       }
   }

   /**
   * The method searchName that takes an array of string type
   * and search word and finds the number of times the word
   * x contains in the string array list and
   * return the count to calling method.
   * */
   public static int searchName(String[] list, String x) {      

       //set count=0
       int count=0;
       for(int i=0;i<list.length;i++)
       {          
           if(list[i].contains(x))
           {
               //increment count by 1
               count=count+1;      
           }
       }
       //return count
       return count;
   }//end of method searchName

}//end of the class

------------------------------------------------------------------------------------

Sample output:

Enter number of words to read from keyword
3
Enter name:apple
Enter name:apple
Enter name:orange
Enter the word you want to search:apple
The word exists in the Array 2 times

Add a comment
Know the answer?
Add Answer to:
write a program which include a class containing an array of words (strings).The program will search...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • Modification of original code so it can include 1) at least one array 2)*New English words...

    Modification of original code so it can include 1) at least one array 2)*New English words that are written/saved to the .txt file(s) must be in alphabetical order* (e.g, Apple cannot be in a lower line than Orange). This is what I'm really struggling with as I don't know how to organize alphabetically only the english word translation without the spanish equivalent word also getting organized. The only idea I have right now is to recreate the code using two...

  • Information About This Project             In the realm of database processing, a flat file is a...

    Information About This Project             In the realm of database processing, a flat file is a text file that holds a table of records.             Here is the data file that is used in this project. The data is converted to comma    separated values ( CSV ) to allow easy reading into an array.                         Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Summary You will write an application to build a tree structure called Trie for a dictionary...

    Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT