//*********Program Plan*********
//Add each element of the word.txt in LinkedList if it does not exist.To find the code word , find the index of phrase in //linked list and find the next element of the index
//*************Code****************
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
public class CodePhrase {
public static void main(String[] args) {
// declare wordLinkedList to store the words
LinkedList<String> wordLinkedList = new LinkedList<String>();
// declare and initialize the codedPhraes to store the output
String codedPhrase = “”;
// declare phrase string variable to read the input entered
String phrase;
// call function readWordTextFile which read file and return LinkedList
// store the linkedlist return from function call readWordTextFile()
wordLinkedList = readWordTextFile();
System.out.println(“Please enter a phrase”);
try {
// buffer reader to read input
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// read whole phrase in one line and store into phrase
phrase = bufferedReader.readLine();
// declare and initlise the splitedPhrase array to store the each word of phrase
String[] splitedPhrases = phrase.trim().split(” “);
// for loop to iterate over all the word of the entered phrase
for (int i = 0; i < splitedPhrases.length; i++) {
// find index of word in wordLinkedList
int index = wordLinkedList.indexOf(splitedPhrases[i].trim());
// index =-1 mean word does not exists in word LinkedList
if (index != -1) {
// check if the word is not last element of the wordLinkedList
if (index != wordLinkedList.size() – 1)
// then code word for this word is next element(i+1);
codedPhrase = codedPhrase + wordLinkedList.get(index + 1);
else
// if the word is last element of the wordLinkedList
// then add the first element as its code word
codedPhrase = codedPhrase + wordLinkedList.get(0);
// seprate the coded word by a space
if (i < splitedPhrases.length – 1)
codedPhrase = codedPhrase + ” “;
}
}
System.out.println(“nCoded phrase is: “);
System.out.println(codedPhrase);
} catch (IOException e) {
// catch the exception
e.printStackTrace();
}
}
// function to read the word.txt file
public static LinkedList<String> readWordTextFile() {
// declare wordLinkedList to store the words
// which is to be return by this function to its caller
LinkedList<String> wordLinkedList = new LinkedList<String>();
try {
// Open the file
FileInputStream fstream = new FileInputStream(“words.txt”);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// declare word
String word;
// Read File Line By Line
while ((word = br.readLine()) != null) {
// if word does not exist in the wordLinkedList
// then add the word in the wordLinkedList
if (isWordExist(wordLinkedList, word) == false)
wordLinkedList.add(word);
}
// Close the input stream
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return the wordLinkedList and exit from the function
return wordLinkedList;
}
// function to check if the word exisit in linkedlist
public static boolean isWordExist(LinkedList linkedList, String word) {
// if word exists in linkedlist then return true else return false
if (linkedList.contains(word))
return true;
else
return false;
}
}
//*****************Input/Output Screenshot**********
//word.txt
cat
cat
dog
mouse
dog
horse
deer
elephant
deer
mouse
monkey
//*****Output****
Please enter a phrase
dog horse elephant monkey
Coded phrase is:
mouse deer monkey cat
