Hi, I am trying to create a program that reads a .txt file and finds any abbreviations and if found puts <> around it. However, I am having a problem that if a word has a "u" like "you" it will print "yo<u>". I only want it to do that when only "u" is present and such.
I am new to this and any help will be greatly appreciated! Thanks!
Here is my code:
import java.util.*;
import java.io.*;
public class TextFileInputDemo
{
public static void main(String[] args)
{
//file must already exist to be read by program.
String fileName = "file.txt";
Scanner inputStream = null;
System.out.println("The text message reads:");
try
{
inputStream = new Scanner (new File(fileName)); //Import files
}
//file not found
catch(FileNotFoundException e)
{
System.out.println("Error opening the file : " + fileName);
System.exit(0);
}
//read files and finds abbreviations.
while(inputStream.hasNextLine())
{
String line = inputStream.nextLine();
line = line.toLowerCase();
if (line.contains("")) //reads every line
{
line = line.replaceAll("lol", "<lol>");
line = line.replaceAll("iirc", "<iirc>");
line = line.replaceAll("4", "<4>");
line = line.replaceAll("ttfn", "<ttfn>");
line = line.replaceAll("wbu", "<wbu");
line = line.replaceAll("dis", "<dis>");
line = line.replaceAll("ily", "<ily>");
line = line.replaceAll("aka", "<aka>");
line = line.replaceAll("wrud", "<wrud>");
line = line.replaceAll("wit", "<wit>");
line = line.replaceAll("u", "<u>");
line = line.replaceAll("nm", "<nm>");
line = line.replaceAll("r", "<r>");
System.out.println(line);
}
else //Line has no abbreviations.
{
System.out.println(line);
}
}
inputStream.close();
}
}