Question

In: Computer Science

A Java program that deciphers each message by building the string. Each line of the text...

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

Solutions

Expert Solution

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


Related Solutions

Please create a Python program that can convert a text string to an encrypted message and...
Please create a Python program that can convert a text string to an encrypted message and convert the encrypted message back to the original message. Read a string message from your keyboard. Print the input string. Convert this message into an encrypted message by using a list element substitution. Print your encrypted message. Convert this encrypted message back to the original message by using a list element substitution. Print your original message. Important: please use a dictionary data type and...
C++ Text message decoder Use getline() to get a line of user input into a string:...
C++ Text message decoder Use getline() to get a line of user input into a string: Enter text: IDK if I'll go. It's my BFF's birthday. Search the string using find() for common abbreviations and replace() them with their long form. In addition, use a for loop to iterate through each character of the string and replace any occurences of '.' with '!': Enter text: IDK if I'll go. It's my BFF's birthday. I don't know if I'll go! It's...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of user input into a string. Output the line. (3 pts) Ex: Enter text: IDK if I'll go. It's my BFF's birthday. You entered: IDK if I'll go. It's my BFF's birthday. (2) Search the string (using find()) for common abbreviations and print a list of each found abbreviation along with its decoded meaning. (3 pts) Ex: Enter text: IDK if I'll go. It's my...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
Write a program that reads a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt java program Write...
Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt java program Write the following string ‘Saya suka belajar OOP’ to text file name suka.txt
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT