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 6 of 6

Thread: Replacing words in a text file

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Replacing words in a text file

    Hello. I am going to be reading from a .txt file line by line in which the lines will have "fill in the blanks" so to say. For example, one of the lines of text might read

    both of you <verb> <adverb> to the left. Next, you and

    In this case the line would be read, then it will prompt the user for a verb and then replace <verb> with the users input. It would then ask for an adverb and replace the <adverb> with the users next input and so on throughout the entire .txt file. Right now I have

    if(line.toLowerCase().indexOf("plural-noun")!=-1)element="plural-noun";
    else if(line.toLowerCase().indexOf("verb")!=-1)element="verb";
    else if(line.toLowerCase().indexOf("adjective")!=-1)element="adjective";
    else if(line.toLowerCase().indexOf("adverb")!=-1)element="adverb";
    else if(line.toLowerCase().indexOf("noun")!=-1)element="noun";
    else if(line.toLowerCase().indexOf("name")!=-1)element="name";
    else if(line.toLowerCase().indexOf("noise")!=-1)element="noise";
    else if(line.toLowerCase().indexOf("number")!=-1)element="number";
    else if(line.toLowerCase().indexOf("place")!=-1)element="place";
    else return;
    System.out.print("Please type a "+element+": ");
    String userInput=in.next();
    line = line.toLowerCase().replace("<"+element+">",userInp ut);

    Is there any way that I could just always get the word in between the brackets < >? Thanks!


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Replacing words in a text file

    If you focus on where each opening and closing bracket is on the line, you should be able to figure out whats in between.

  3. The Following User Says Thank You to marylandfour For This Useful Post:

    sanelko (January 30th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Replacing words in a text file

    Thanks, well I tried to do that and now I'm getting an error I just can't seem to figure out.
    I try to run

    int openIndex = line.indexOf("<");
    int closeIndex = line.indexOf(">");
    String marker = line.substring(openIndex+1, closeIndex - openIndex);
    System.out.print("Please type a "+marker+": ");
    String userInput=in.next();
    line = line.replace("<"+marker+">",userInput);

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -8
    at java.lang.String.substring(Unknown Source)
    at MadLibs.Game(MadLibs.java:108)
    at MadLibs.Create(MadLibs.java:63)
    at MadLibs.MadLibs(MadLibs.java:33)
    at MadLibs.main(MadLibs.java:21)

    This is the first line this runs on "I wannabe a <noun> when I grow up."
    Any help would be appreciated

  5. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Replacing words in a text file

    From what I see, you're finding the brackets right, but you should take a second look at how you're trying to grab the stuff in between it.

    This should help you out.

    http://www.javascriptkit.com/javatutors/string4.shtml
    Last edited by marylandfour; January 30th, 2012 at 12:29 AM.

  6. The Following User Says Thank You to marylandfour For This Useful Post:

    sanelko (January 30th, 2012)

  7. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Replacing words in a text file

    Oh I see. so the open index is at 9 and the closed index is 14 thus the marker goes from <9,5> and substring can't go backwards. I can't think of another way to do it though...

  8. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Replacing words in a text file

    Wait I got it, that was really simple haha. Thanks for the help I really appreciate it!

Similar Threads

  1. Removing duplicate words from a file
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 13th, 2012, 05:34 AM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  3. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 09:11 AM
  4. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM