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

four letter words file: http://uahcs.org/CS121/data/dictionary_four_letter_words

ID: 3710627 • Letter: F

Question

four letter words file:

http://uahcs.org/CS121/data/dictionary_four_letter_words.txt

2. (30 Points) Using the dictionary of four letter words write a program that will do the following 1. Open the dictionary file and read all the words storing them in a one dimensional array 2. loop 1. Prompt the user to enter a single lower case letter (a through z 2, Read in the letter 3. Create a new dynamic array containing all the words that begin with that letter. 4. Print the words from that array to a new output file. (ten words per line) 1. (Challenge: Can you make the program create the output file name based on the letter? e.g file of words _beginning with_b.txt) 5. Prompt the user to enter another letter or enter Q (uppercase Q) to terminate the program 3. End looD

Explanation / Answer

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Driver {

public static void main(String[] args) throws IOException {

List<String> wordsarray = new ArrayList<String>();

List<String> ResultArray=new ArrayList<String>();

String letter=null;

int i=1;

while(i==1)

{

Scanner scanner = new Scanner(System.in);

System.out.println("enter the choice");

System.out.println("1.read the file");

System.out.println("2.read the letter");

System.out.println("3:create dynamic array");

System.out.println("4:print");

System.out.println("5.exit");

int choice = scanner.nextInt();

switch (choice) {

case 1:

BufferedReader reader = new BufferedReader(new FileReader(new File(

"D:\words.txt")));

String line = null;

while ((line = reader.readLine()) != null) {

String[] words = line.split(" ");

for (String word : words) {

wordsarray.add(word);

}

}

case 2:

System.out.println("enter the letter to findout");

letter=scanner.next();

System.out.println("the letter is "+letter);

  

break;

  

  

case 3: for(String word:wordsarray)

{

if(word!=null)

{

  

char wordfinder=(char)word.charAt(0);

  

if(letter.equals(Character.toString(wordfinder)))

{

ResultArray.add(word);

}

  

}

}

  

break;

  

case 4:PrintWriter writer=new PrintWriter(new File("D: esult.txt"));

int count=0;

for(String result:ResultArray)

{

if(count!=10)

{

count++;

writer.write(result);

writer.write(" ");

writer.flush();

}

else{

count=0;

writer.println();

}

}

writer.close();

break;

case 5:System.out.println("enter for letter for quitting Q or for not Quitting N");

  

String letter1=scanner.next();

if(letter1.equalsIgnoreCase("Q"))

{

System.exit(0);

}

else

System.out.println("contitue");

break;

}//end of switch

}

}

}

enter the choice
1.read the file
2.read the letter
3:create dynamic array
4:print
5.exit
1
enter the letter to findout
a
the letter is a
enter the choice
1.read the file
2.read the letter
3:create dynamic array
4:print
5.exit
3
enter the choice
1.read the file
2.read the letter
3:create dynamic array
4:print
5.exit
4
enter the choice
1.read the file
2.read the letter
3:create dynamic array
4:print
5.exit
5
enter for letter for quitting Q or for not Quitting N

Q

result.txt

aahs aals abas abba abbe abed abet able ably abos  
abut abye abys aced aces ache achy acid acme acne  
acta acts acyl adds adit ados adze aeon aero aery  
agar agas aged agee ager ages agha agin agio agly  
agog agon ague ahed ahem ahis ahoy aide aids ails  
ains airn airs airt airy aits ajar ajee akee akin  
alan alar alas alba albs alec alee alef ales alfa  
alif alit alky alls ally alma alme alms aloe alow  
also alto alts alum amah amas ambo amen amia amid  
amin amir amis ammo amok amps amus amyl anal anas  
anes anew anga anil anis ankh anna anoa anon ansa  
ante anti ants anus aped aper apes apex apod apos  
apse aqua arak arbs arch arco arcs area ares arfs  
arid aril arks arms army arse arts arty arum arvo  
asci asea ashy asks asps atap ates atma atom atop  
auld aunt aura auto aver aves avid avos avow away  
awee awes awls awns awny awol awry axal axed axel  
axil axis axle axon ayah ayes ayin azan azon