*Help Replace a word in a string (RemoveString)
Author |
Message |
Daveliang99
|
Posted: Sat Nov 01, 2008 9:01 pm Post subject: *Help Replace a word in a string (RemoveString) |
|
|
Hello, i am a beginner in Java and i need some help with the following exercise.
Create a RemoveString application that prompts user for a sentence and a string. The application should remove every occurrence of string from the sentence.
Here is an example of my code, i know its missing the some kinda key components.
Java: |
//Problem that it removes the whole sentence and only keeps the first character
//I'd like the program to remove the word and keep the remaining sentence the same
//Please suggest me which type of "replacement syntax" i should use and modify my code
import java.util.*;
import java.io.*;
import java.lang.String;
public class StrRemov {
public static void main (String[] args ) {
String str, result, strreplace;
Scanner scan = new Scanner (System. in);
System. out. print ("Enter Sentence: ");
str = scan. next();
strreplace = "really";
result = str. replaceAll(" ", strreplace );
System. out. println(result );
}
} |
Thank you very much and your help is much appreciated
Please respond as soon as possible. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Sat Nov 01, 2008 10:25 pm Post subject: Re: *Help Replace a word in a string (RemoveString) |
|
|
Hello Daveliang99,
Yeah, the members of compsci.ca are unlikely to do homework assignments for others. Indeed, we may harass people that do so.
For now, a hint;
Take a good look at the replaceAll documentation. Think carefully about what the search pattern is and what the replacement value is. With a caution; the search pattern is not a simple string, requires special care and handling.
Cheers |
|
|
|
|
![](images/spacer.gif) |
delparnel
![](http://compsci.ca/v3/uploads/user_avatars/980326151490c8c77b2278.jpg)
|
Posted: Sat Nov 01, 2008 10:32 pm Post subject: Re: *Help Replace a word in a string (RemoveString) |
|
|
Hi Daveliang99,
Please use the code blocks when posting code. If you don't, it's very hard to read.
btiffin is correct. Another hint is to look at the Scanner documentation.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
The next() method simply gets the next token, which might not be your objective. |
|
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Sun Nov 16, 2008 9:41 pm Post subject: RE:*Help Replace a word in a string (RemoveString) |
|
|
I'm new to Java and I don't know much about this kind of stuff, but after reading the documentation, I can't really find anything. I played around with it though and if you don't have any whitespace characters, it's all good and it works. I'm pretty sure this has something to do with Scanner only accepting one word as the whole string. Kind of like in Turing when you use get, it only takes one word unless you put get : *. |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Sun Nov 16, 2008 11:15 pm Post subject: RE:*Help Replace a word in a string (RemoveString) |
|
|
Simple take the string that you get in, then break it up somehow [ java has a function for that; illl let you find it] then store each word into an array of strings; compare and re output the string. |
|
|
|
|
![](images/spacer.gif) |
Einherjar
![](http://compsci.ca/v3/uploads/user_avatars/8614838684920d34d60982.jpg)
|
Posted: Mon Nov 17, 2008 12:01 am Post subject: RE:*Help Replace a word in a string (RemoveString) |
|
|
Use nextLine ();
next(); only read the until the first space.
Also, if you replace "really", not " really" or "really ", you'll end up with more than 2 spaces. now that you add a " " to it, you'll have 3.
EDIT: result = str.replaceAll(" ", strreplace); would replace all " " with "really", so I don't think that's what you're aiming for...
Read the documentation more carefully. |
|
|
|
|
![](images/spacer.gif) |
|
|