Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Replacing parts of a String using line.replaceAll

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Replacing parts of a String using line.replaceAll

    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();
    }

    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Replacing parts of a String using line.replaceAll

    Look at java.util.regex.Pattern and change the first argument to replaceAll so that it's like "[not a valid word character]u[not a valid word character]" to solve your problem

Similar Threads

  1. replaceAll() help
    By M0dSe7en in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2012, 07:30 AM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  4. [SOLVED] replacing elements in a string
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 07:31 PM
  5. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM

Tags for this Thread