Hi i have to make this program that will read a message from another text file and surround each occurence of an abbreviation with <> brackets. and the write the marked message to a different file. The program must beable to read from a text file that contains the abbreviations and a text file that contains the message. Could someone please tell me if I am on the right track?

package abbreviationstest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.FileNotFoundException;

/**
 *
 * @author 420
 */

public class abbreviationsTest
{
    public static void main(String[] args) 
    {
        String fileName = "abbreviations.txt";
        Scanner inputStream = null;
        System.out.println("The file " + fileName + " contains the following lines:");

        try
        {
            inputStream = new Scanner(new File(fileName));
            //messageInputStream = new Scanner(new File(fileName));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file " + fileName);
        }
        while (inputStream.hasNextLine())
        {
            String line = inputStream.nextLine();
            System.out.println(line);
        }
        inputStream.close();

        String message = "message.txt";
        //FileOutputStream// messageOutputStream =
        try
        {
            FileOutputStream outputStream = new FileOutputStream(new String(message));
            System.out.println("The file " + message + " contains the following lines:");
            Scanner keyboard = new Scanner(new File(message)); 

            String newMessage = message;            
            do
            {
                newMessage = keyboard.next();
                outputStream.toString();
            }
            while(newMessage.matches(message));
            {
                System.out.print(newMessage);
            }
        }
        catch(FileNotFoundException e1)
        {
            System.out.println("Error opening the file " + message);
        }

    }
}

So, besides you are not coding the requirements regarding abbreviations, what problems are you having?

I get the following error when I run the program:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at testa6q1.TestA6Q1.main(TestA6Q1.java:49)
Java Result: 1

and im not sure if I must make the abbreviations.txt go into an array or the message.txt go in one, what would be the best way to add the <>brackets to the message and save it into a new text file

If you think about it...
You need to know what all the abbreviations are before you can start processing the text file. So the overall plan needs to be:

read all the abbreviations into an array or other data structure
for each line in the text file
  search the line for any abbreviations
     add the < and > for any abbreviations you find
  write the updated line to the output file

Don't try to do it all in one go. Real programmers test their code early and often. Eg:
Write the code for "read all the abbreviations into an array", then add some temporary code to print out that array, and run the program to test that you have tread the abbreviations correctly. Don't go any further until you have fixed any bugs.
Then write and test the code to read the text file line by line, and write them (unchanged for now) to the output file. Run that and get it working.
Finally, do the code to find and mark the abbreviations.

By working like that you will find and fix your bugs one at a time, which is by far the easiest way. You will also have the satisfaction of seeing results and progress as you work.

commented: Thanks +0

So basically what I am suppose to be doing to bring in the abbreviations into the an array, then read the message, compare all the abbreviations to the abbreviations present in the message then add the brackets to the present abreviations then only I can rewrite the file with a different name.

Yes.
But you don't need to process the whole message in one go, just read/process/write one line at a time. Its easier that way.

Can I use compareTo() to compare the abbreviations in the array to the message in the .txt file and toString() to place the <>brackets around the abbreviation present in the file

You're looking for the abbreviations anywhere in the String, so cpmparteTo isn't useful for that. Have a look at the String class, indexOf and replace methods.

I changed the code completely and and used a replace all method, if I use the indexOf method what should what should the index start with.

package retest;
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;

/**
 *
 * @author 420
 */
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);
            }
            while(fileInputStream.hasNextLine())
            {
                String line1 = fileInputStream.nextLine();
                System.out.println(line1);


            if(line1.contains(abbrev))
            {
                line1.replaceAll(abbrev,"<" + 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);
        }

    }
}

Don't worry about indexOf, replaceAll is the better option. If you use indexOf you will also have to a load of substrings to do the replacement.
replaceAll is the easiest solution - but there is a catch! The first parameter isn't an ordinary string, it's a regular expression, so if the abbreviation contains any REGEX reserved characters (eg - or . ) they will cause problems.
the simpler replace method just replaces one occurrence, and doesn't use a regex, but you'll have to use it in a loop to replace multiple occurrences.

I have used the replace method in a do-while loop however it doesn't put in the <>brackets

Without seeing the relevant code there's absolutely nothing I can say.

I got it to put the <>brackets however it does each abbrviation in a line of it's own and not just a single line. and how would one close the first while loop and if statement and still have the variables available through out the program.

package retest;
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;

/**
 *
 * @author 420
 */
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);
        }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.