Can someone write the coding for me please ??? here is the question Text messagi
ID: 3644289 • Letter: C
Question
Can someone write the coding for me please ??? here is the questionText 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! <:)>
can you sent the coding to my email please to javasteve3@gmail.com as text format..... thanks a lot
Explanation / Answer
Tricky part here is forming the regular expression. I wasn't able to get a Pattern.Quote() to work right with a case insensitive search, so I used the input string to create an equivalent, creating a character class for the upper & lower of each char in the string. import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Pattern; public class AbbrevLocator { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader abbrevs = null; BufferedReader message = null; BufferedWriter output = null; try { abbrevs = new BufferedReader(new FileReader("C:\abbrevs.txt")); message = new BufferedReader(new FileReader("C:\message.txt")); output = new BufferedWriter(new FileWriter("C:\output.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String msg = ""; try { while (message.ready()) { try { msg = msg + message.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String abr = ""; try { while (abbrevs.ready()) { String pattern = "\W"; abr = abbrevs.readLine().trim(); for (char n : abr.toCharArray()) { pattern = pattern + "[" + (n + "").toLowerCase() + (n + "").toUpperCase() + "]"; } Pattern p = Pattern.compile(pattern); msg = msg.replaceAll(p.toString(), " "); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(msg); try { output.write(msg); output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.