• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Substituting strings

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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("");
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Layne
[ June 04, 2005: Message edited by: Layne Lund ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic