?
Solved

JAVA programming problem

Posted on 2003-02-21
3
Medium Priority
?
192 Views
Last Modified: 2010-03-31
I need help writing a script where a user can input values via keyboard, lets say the user enters " I hate to hate you".  The script should echo what the user wrote, then echo and change the first "hate" to "love".  Only the first hate can change, no matter how many "hate" exist.

The final output should look like " I love to hate you"

If you can solve this one... you are truly a programming god!
0
Comment
Question by:angeljava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 7997851
import java.io.*;

public class Example {

     public static void main(String[] args) {
          String text = null;

          try {
               System.out.print("Enter text: ");
               BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

               if ((text = in.readLine()) != null) {
                    int ix = text.indexOf("hate");
                    if (ix != -1) {
                         text = text.substring(0, ix) + "love" + text.substring(ix+4);
                    }
                    System.out.println(text);
               }

               in.close();
          } catch (Exception e) {
               e.printStackTrace();
          }

     }

}
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 7998313
String text = "I hate to hate you";
String newText = new String();

int pos = text.indexOf("hate");
if (pos != -1)
{
   newText = text.substring(0,pos);
   newText = newText + text.substring(pos+4);
   System.out.println(newText);
}
else
{
   System.out.println("Not found");
}
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 7998321
String text = "I hate to hate you";
String newText = new String();

int pos = text.indexOf("hate");
if (pos != -1)
{
  newText = text.substring(0,pos);
  newText = newText + " love " + text.substring(pos+4);
  System.out.println(newText);
}
else
{
  System.out.println("Not found");
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question