jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Substituting strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Substituting strings" Watch "Substituting strings" New topic

Substituting strings

Keith Riordan

Joined: Jun 04, 2005
Posts: 6
Hey ya'll, java noobie here

I've got a program which recieves three strings(wordOne and wordTwo(each one word)), and sentence(a sentence which contains wordOne)).

I'm trying to figure out how to make the program acknowledge [wordOne] in [sentence], replace it with [wordTwo] and output the sentence now containing wordTwo in place of wordOne.

I'm trying to figure out how to extract wordOne using substring but I've hit a wall. Any help would be greatly appreciated, thx.
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi, and welcome to the JavaRanch.

I'd suggest you take a look at the StringTikenizer class ( http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html )

and the String's equal method ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html )

The above should do the work. Happy hackin'

/Svend Rost

edit: url problems
[ June 04, 2005: Message edited by: Svend Rost ]
Keith Riordan

Joined: Jun 04, 2005
Posts: 6
Thx Svend but i guess i'm even newer than i thought, i'm completely lost with the classes and methods. Looking at the StringTokenizer class i don't know how to incorporate that into the code.
am i on the right path here:
I have the user enter wordOne, wordTwo, and sentence(w/ wordOne) via the Scanner class(I'm actually on a Mac so I'm using 1.4.2 on Eclipse and i got the scanner class from a professor at Longwood University(my teacher said this was ok) this is probably irrelevant).
Now with the input i don't now how to use the StringTokenizer class to take apart the sentence. And then once it is seperated i dont know what to do with the equals method(or how to use it for that matter), it's boolean(dont really know how to use this) do i somehow tell the program to check all the newly broken up tokens for wordOne answering true or false to each token until it finds wordOne?? Ahh so confused, just had to get this post up, srry if it makes no sense.

Here is what i got so far:

import java.util.*;
import scanner.Scanner;

public class Subtest {

public static void main(String[] args) {
Scanner stdin = Scanner.create(System.in);

String wordOne, wordTwo, sentence;

System.out.println("This program recieves two words and a sentence containing the "
+ "first word from the user. \nThen prints out a sentence using the second word "
+ "in substitute for the first one.");
System.out.println("Enter a word: ");
wordOne = stdin.nextLine();
System.out.println("Enter another word: ");
wordTwo = stdin.nextLine();
System.out.println("Please enter a sentence using your first word: ");
sentence = stdin.nextLine();

StringTokenizer sent1 = new StringTokenizer("");
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
I think I would try to use the replaceFirst() or replaceAll() method of the String class. Something like

String output = sentence.replaceFirst( wordOne , wordTwo );

You would probably use something like
System.out.println( output );
to print the result.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Marilyn's suggestion will work and most professional programmers would probably do it this way. However, it's probably also a good idea to write a method that does this on your own. In fact, I would guess that this might be the purpose of this assignment, if it is indeed a homework assigment.

Whenever you approach a programming problem like this, you should first get out pencil and paper. Write out a description of the steps you need to take in order to accomplish the task. For example, you might do something like this:

Notice that I've used some words that are similar to what you might see in a Java program. That's on purpose! As I was figure out how to do this, I keep all my tools in mind including for loops and if statements. However, I don't worry about the exact syntax. That can come later.

Also most of this cannot be converted directly into Java. If I were to continue, I would elaborate what I mean by "copy all the letters in sentence up to the first one that was matched", for example. In fact, there are several lines that mention copying from one String to another. This seems ideal for a separate method that accomplishes that particular task.

This is only one way to do it. In fact, it's probably not a very good way. However, the point is that when you start on a new homework problem you should write out some ideas before you ever start writing any code.

I hope this helps.

[ June 04, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Here's another idea:

Find the index of wordOne in the sentence.
Change the String into a StringBuffer.
Delete the character at the index position for as many times as there are letters in wordOne.
Insert wordTwo at that same index position.
Change it back to a String and print it.
I agree. Here's the link: http://aspose.com/file-tools
subject: Substituting strings
It's not a secret anymore!