Hello, guys.

I'm very new to Java and this is my first time I'm actually programming by myself. I have a homework due soon.

Anyways, my question is:

Write a program that asks the user for a word to replace in some text, and the word to replace it with. The program then reads a line of text from the user and echoes the line back to screen replacing all occurrences of the first word with the second word. It only replaces the word being censored when it occurs independently in a line (i.e. not as a part of another word).
Enter word to censor: dumb
Enter word to replace it: xxxxx
Enter line of text to censor:
The guy is dumb but his friend is even dumber.
The guys is xxxxx but his friend is even dumber.

import java.util.*;
      public class LineCensor
   public static void main(String args[])
      String sentence = "Please type a word to Censor:";
      Scanner keyboard = new Scanner(System.in);
      String = keyboard.nextDouble();

      String [] temp = null;
      temp = sentence.split(" ");

      for (int j=0; j < temp.length; j++ )
         if (temp[j].equals("bridge"))
           sentence = "Censored";
      System.out.println(sentence + " " + ac);

Thats all I know. Can someone help me with the code ? How do I replace some word to something ?
I know its not compiling either. I'm kind of stuck as this is my first time with java?

Recommended Answers

All 3 Replies

You are only reading the word to censor. You should read the word that will replace the first word you read, as well as the text in which you will search for the word to censor.

I think that the : keyboard.nextDouble(); returns double
You must declare a variable in which you will store the input:
This is wrong: String = keyboard.nextDouble();
This is correct: String s = keyboard. (i don't remember the method'S name)

In this line: System.out.println(altstring); you don't define altstring. You don't give it any value nor you declare it.

The variable sentence should be read from the keyboard. It is the text in which you will search for the word to censor.

You are correct to use the split method and search for the word to censor.
But inside the if you are replacing the entire sentence. Instead you should only replace the censored word:

for (int j=0; j < temp.length; j++ )
 if (temp[j].equals(wordToCensor))
  temp[j] = replacementWord

Don't forget that wordToCensor, replacementWord should be read from the keyboard with Scanner at the begining of the program.

After the for-loop just add another loop that prints the array temp that has the censored words replaced.

thanx for that. I'll try to figure out how to use an array :) for that loop.

Can we also use a do while loop in it ?

You could use a while loop, but with arrays a standard for() loop or a foreach loop is usually preferred.

A do while loop is seldom used to loop an array because a do while always executes at least once before the test and you would have to make sure there were elements in the array before the first iteration. It would just add extra unnecessary code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.