Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2008, 04:50 PM
ing ing is offline
Member
 
Join Date: Apr 2008
Posts: 3
ing is on a distinguished road
Need Help
I'm building a program that allows a user to select a word from a sentence and replace it with a word, which a user also enters. i also want to all in-case sensitive.

So for example:

a user enter the following
sentence: Hello my name is Peter Peters
wordSelect: peters
replacementWord: Pan

Result: Hello my name is Peter Pan

can anyone help?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-15-2008, 02:53 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
This should work. All of my tests worked. Let me know if this is what you need. You'll just have to add in the user input.

Code:
static String repWord(String sentence, String wordSelect, String replacementWord) { String temp; String[] splitSentence; temp = sentence.toLowerCase(); wordSelect = wordSelect.toLowerCase(); splitSentence = temp.split(wordSelect); int count = 0; String finalSentence = ""; for (String x : splitSentence) { finalSentence += sentence.substring(count, count + x.length()) + replacementWord; count += x.length() + wordSelect.length(); } if (temp.lastIndexOf(wordSelect) != temp.length() - wordSelect.length()) { finalSentence = finalSentence.substring(0, finalSentence.length() - replacementWord.length()); } return finalSentence; } public static void main(String[] args) { String sentence = "Hello my name is Peter Peters"; String wordSelect = "peters"; String replacementWord = "Pan"; String output; output = repWord(sentence, wordSelect, replacementWord); System.out.println(output); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 11:44 AM
ing ing is offline
Member
 
Join Date: Apr 2008
Posts: 3
ing is on a distinguished road
Yeah thanks, it great, thanks. Just one question is it possiible to not have the "static String repWord()".
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 12:14 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Possible....


just make an instance of class in your main method and call method on that instance.



sanjeev
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 06:04 PM
ing ing is offline
Member
 
Join Date: Apr 2008
Posts: 3
ing is on a distinguished road
Quote:
Originally Posted by ing View Post
I'm building a program that allows a user to select a word from a sentence and replace it with a word, which a user also enters. i also want to all in-case sensitive.

So for example:

a user enter the following
sentence: Hello my name is Peter Peters
wordSelect: peters
replacementWord: Pan

Result: Hello my name is Peter Pan

can anyone help?
How do make it so that it doesnt replace words that don't match. for example:

sentence: Hello my name is Peter Peters. Peters's
wordSelect: peters
replacementWord: Pan

Result: Hello my name is Peter Pan. Peters's

Thanks
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-17-2008, 06:42 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
You could just make it so that whatever word you go to search for you add a space before and after it. But then if the word comes before a comma or period it wont change it. You would have to make a case statement and have it grab the letter after the word each time and if it is a space, comma, or period allow it, but if it is anything else don't touch it. With this extra overhead it may be more beneficial to add regular expressions into it. Goodluck.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 08:56 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org