File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Help me with my 1st Java Assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me with my 1st Java Assignment" Watch "Help me with my 1st Java Assignment" New topic

Help me with my 1st Java Assignment

Adnan Alvee

Joined: Feb 01, 2013
Posts: 11
My first assignment is from Savitch's book "Absolute Java". Here is the problem:

Q: Write a program that starts with a line of text and then outputs that line of text with the first occurence of "hate" changed to "love". For example, a possible sample output might be

The line of text to be changed is:
I hate you.
I have rephrased that line to read:
I love you.

You can assume that the word "hate" occurs in the input. If the word "hate" occurs more than once in the line, your program will replace only the first occurrence of "hate". Since we will not discuss input until chapter 2, use a defined constant for the string to be changed. To make your program work for another string, you should only need to change the definition of this defined constant.
Jamie Laurent

Joined: Oct 06, 2011
Posts: 14
My first attempt at helping someone.. But my suggestion would be to search online for the Java API and browse for the different String methods that exist to see if any of them can get you going. Then after some trial and error come back with a more specific question with the code you tried.

Here is a link:
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Where's your code? What problems are you having with it?

[Asking smart questions] [About Bear] [Books by Bear]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46409
Joe Ridener:-
Please read this, which appears on the title page for this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
If you provide a complete answer early on, you are actually preventing the OP) from actually learning.
We might restore your post later.
Dinesh Kumar Ramakrishnan
Ranch Hand

Joined: Aug 07, 2012
Posts: 61

you can use replaceFirst(String regex,String replacement) method for your problem


refer api Documentation for more method details...,

“If you’re good at something, never do it for free.” —The Joker
Adnan Alvee

Joined: Feb 01, 2013
Posts: 11
Bear Bibeault wrote:Where's your code? What problems are you having with it?

I'm really confused, thought of using substring since it is mentioned in the chapter 1, but I dont know how
this is what i got

public class TextCHange {

public static void main(String[] args) {

String Text1 = "I hate you and they love you";


Please help, I need to submit it ASAP!!
Jamie Laurent

Joined: Oct 06, 2011
Posts: 14
As i stated before go to the link i put in my first reply and look at the different String methods you can use. There are plenty of examples there to help you. From what I have gathered, it is frowned upon to just give you answers especially in regards to doing your homework. You will not learn it if someone else gives it to you.

The best way that i found and is only 3 lines of code is just to use the String method replacefirst as the last person mentioned. You can test it by adding more "hate" parts to the initial phrase to make sure it doesnt replace more than the first occurrence.
I agree. Here's the link:
subject: Help me with my 1st Java Assignment
It's not a secret anymore!