Part A -Text messaging is a popular means of communications. Many abbreviations
ID: 3557497 • Letter: P
Question
Part A-Text messaging is a popular means of communications. Many abbreviations are in common use but are not appropriate for formal communications. Suppose the abbreviations are stored, one to a line, in a text file names abbreviation.txt. For example, the file might contain these lines:
lol
:)
iirc
4
u
ttfn
Write a program that will read a message from another text file and surround each occurrence of an abbreviation with <> brackets. Write the marked message to a new text file.
For example, if the message to be scanned is
How are u today? Iirc? This is your first free day. Hope you are having fun! :)
The new text file should contain
How are <u> today? <Iirc>, this is your first free day. Hope you are having fun! <:)>
Part B- Write an application or applet that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in the JTextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the original file. Create an Exit button to exit the application.
Technical Details: Read each line from the file and then display the line in the text area using the method append (aString). The method getText will return all of the text in the text area in a string that then can be written to the file. The following statement will make the text referenced by editorTextArea scrollable:
JScrollPane scrollPane = new JScrollPane(editorTextArea);
Then add scrollPane to your GUI, just as you would add any other component. The text area editorTextArea does not need to be added to the GUI, but it can be used.
Part C
Explanation / Answer
part A
import java.util.*;
import java.io.*;
public class Generator
{
public static Hashtable ht = new Hashtable();
public static File myText = new File("chat.txt");
public static String dictionary = "abbreviation.txt";
public static void main(String[]args)
{
try
{
FileInputStream fstream = new FileInputStream(dictionary);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read the dictionary line by line
while ((strLine = br.readLine()) != null)
{
strLine.trim();
ht.put(strLine.hashCode(),strLine);
}
//Close the input stream
in.close();
//write the output to a file
File file = new File("output.txt");
FileWriter fw = new FileWriter(file);
Writer wr = new BufferedWriter(fw);
//Now prepare to look for words in given chat message and add tags
//Scanner will be used to read the chat file, word by word
Scanner input = new Scanner(myText);
while(input.hasNext())
{
String word = input.next();
int key = word.hashCode();
boolean exists = ht.containsKey(key);
//code to add tags
if(exists == true)
{
String symbol1 = "<";
String symbol2 = ">";
word = symbol1.concat(word);
word = word.concat(symbol2);
}
//code to write the output file
wr.write(" ");
wr.write(word);
}
wr.close();
}catch (Exception e){System.err.println("Error: " + e.getMessage());}
}
}