Results 1 to 9 of 9

Thread: Simple problem

  1. #1
    melovehockey is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Simple problem

    Hey everyone, I am very new to Java and I am learning how to change a specific word from a line of text that is inputted by the user. For example, if I created the program to change the word "hate" to the word "love" (which is what the program is doing) how can I make the program i created more efficient?

    This is the main portion of my program...

    Java Code:
    String sentence, subsentence;
    		int position;
    
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please enter a line of text.");
    		System.out.println("I will change the word hate to the word 
                    love:");
    
    		sentence = keyboard.nextLine();
    		subsentence = sentence;
    
    		System.out.println("I am now checking to see if \"hate\" is in 
                    your sentence.");
    		System.out.println("...");
    		System.out.println("..");
    		System.out.println(".");
    		System.out.println(sentence + " is now changed to");
    		position = subsentence.indexOf("hate ");
    
    		subsentence = subsentence.substring(0, position) + "love";
    		position = position + 4;
    		sentence = sentence.substring(position);
    		System.out.println(subsentence + sentence);
    NOTE: I feel like there should be a way to do this program without having to create a seperate String called subsection. Thanks for the help, it is greatly appreciated!
    Last edited by Eranga; 12-28-2010 at 12:58 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Before that, if user enter a value "hate" how your program detect it? Then how you match the relevant response?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    melovehockey is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Yes, I just want to replace the word "hate" with "love". Before I added the second String (subsentence), The program would still replace "hate" with "love" but it would stop the sentence right there. So if I had "I hate this program" it would only show up as "I love" and not finish the rest of the sentence

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Read the String API. There is a method call replaceAll, where you can replace words/letters, in a single line.

    The way you tried is too complex. Give a try and let me know if you stuck with.

  6. #6
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    use the String method replace() or replaceAll() to replace text.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    melovehockey is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Ok great, thanks guys. I'll give it a shot and see if it works. You guys must be tired of all of these questions like this, hopefully one day i'll help you guys out!

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by melovehockey View Post
    Ok great, thanks guys. I'll give it a shot and see if it works. You guys must be tired of all of these questions like this, hopefully one day i'll help you guys out!
    It's really nice. Try to takeout the best, and give back more than that.

Similar Threads

  1. Very simple problem
    By Andyj in forum Java Certification
    Replies: 3
    Last Post: 09-17-2010, 06:49 AM
  2. I have a simple problem
    By o0oNorao0o in forum JCreator
    Replies: 20
    Last Post: 12-25-2009, 08:32 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. simple GUI problem
    By McChill in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 03:13 AM
  5. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •