Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 *Help Replace a word in a string (RemoveString)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Daveliang99




PostPosted: 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 Very Happy

Please respond as soon as possible.
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: 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
delparnel




PostPosted: 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.
andrew.




PostPosted: 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 : *.
syntax_error




PostPosted: 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.
Einherjar




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: