Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a Java homework question - I have an idea of how this should be solved bu

ID: 662506 • Letter: I

Question

I have a Java homework question - I have an idea of how this should be solved but it would be nice to have correct code to compare mine with to figure out where I am going wrong. Thank you in advance.

----------------------

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! <:)>

Explanation / Answer

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;


public class Retest
{
    public static void main(String[] args)
    {
            String abbreviation = "abbreviations.txt";
        try
        {
            Scanner inputStream = new Scanner(new File(abbreviation));

            String line = inputStream.nextLine();

            while(inputStream.hasNextLine())
            {
                line = inputStream.nextLine();

                String[] array = line.split(" ");

                String abbrev = array[0];

                System.out.println(abbrev);

        String fileName = "message.txt";
        Scanner fileInputStream = null;
        System.out.println("The file " + fileName + " Contains the following line: " );

            try
            {
                fileInputStream = new Scanner(new File(fileName));
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Error opening the file " + fileName);
                System.exit(0);
            }
            if(fileInputStream.hasNext())
            {
               String line1 = fileInputStream.nextLine();
               System.out.println(line1);
//               System.exit(0);  

            do
            {
                System.out.print(line1.replaceAll(line, "<"+ abbrev+">"));
            }
            while(line.equals(line1));
            {
            //System.out.println(" hi " + abbrev);
            //System.out.println( line1);
            }
            }
            }

        }
        catch(FileNotFoundException e)
        {
            System.out.println("Cannot find file " + abbreviation);
        }
        catch(IOException e)
        {
            System.out.println("Problem with input from file " + abbreviation);
        }

    }
}