In: Computer Science
A Java program that deciphers each message by building the string. Each line of the text file contains either a word or a command followed by a forward slash and the requirements of the command. A Stack Implementation is needed as. As you read the data from the text file, you need to know if it is a new phrase or command.
The commands are:
i – an insert followed by the letters, numbers, or symbols that need to be inserted into the phrase, followed by the start index where the characters are to be inserted.
d – a delete followed by the start index and end index, deletes all the characters from the start index up to but not including the ending index.
a – an append followed by the letters to be added to the end on the phrase.
u – an undo of the previous instruction (does not undo the instructions p, c, u, or the initial phrase), multiple undo’s in a row are possible.
p – print the current phrase as it is or a completed message after all the instructions.
c – clear the message decoder, to start over with a new phrase and instruction set.
empty line or space – separates each phrase.
Example (txt file):
Hello
a/m
i/p /3
p
d/3/4
u
a/e
d/5/7
p
c
vintage
i/i/4
d/7/8
a/g
p
u
i/e/1
d/2/3
i/n/5
d/6/7
p
c
Output Example:
Help lom
Help Me
Glue
get Blue
Find the java code below. Comments are provided for each line for better understanding.
The given output for the second phrase is wrong. The actual output is attached at the end of the answer.
// imports
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
// to store all phrases so that we can undo as many times as needed
Stack<String> phraseStack = new Stack<String>();
// required to handle file io exception
try {
// replace path with your file path
File myFile = new File("/var/www/html/test/oct/filename.txt");
// create a scanner object from file
Scanner myReader = new Scanner(myFile);
// read file line by line
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
// declare phrase variable to store the string that we operate on
String phrase = null;
// check if it is a command with "/"
if(data.contains("/")) {
// split the command by "/"
String[] temp = data.split("/");
// check for insert or append or delete
switch (temp[0]) {
// insert case
case "i":
// load stack top into phrase
phrase = phraseStack.peek();
// insert the given string
phrase = phrase.substring(0, Integer.parseInt(temp[2])) +
temp[1] +
phrase.substring(Integer.parseInt(temp[2]), phrase.length());
// add phrase to the stack
phraseStack.add(phrase);
break;
// delete case
case "d":
// load stack top into phrase
phrase = phraseStack.peek();
// delete from start index to end index
phrase = phrase.substring(0, Integer.parseInt(temp[1])) +
phrase.substring(Integer.parseInt(temp[2]), phrase.length());
// add phrase to the stack
phraseStack.add(phrase);
break;
// append case
case "a":
// load stack top into phrase
phrase = phraseStack.peek();
// append given characters
phrase += temp[1];
// add phrase to the stack
phraseStack.add(phrase);
break;
default:
break;
}
// operations such as p, u, c
} else if (data.length() == 1) {
switch (data) {
// undo -- delete stack top, so that we get the previous phrase
case "u":
phraseStack.pop();
break;
// print -- print stack top
case "p":
System.out.println(phraseStack.peek());
break;
// clear -- clear stack
case "c":
phraseStack.clear();
break;
default:
break;
}
// data that we read is the phrase
} else {
// add phrase to the stack
phraseStack.add(data);
phrase = phraseStack.peek();
}
}
// close the reader
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Find the output screenshot below.
Note: Make sure to change the file path in the program before you run.
Thanks & Regards