This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

Help me with my 1st Java Assignment

Adnan Alvee
Greenhorn

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
Greenhorn

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: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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 Yadav
Greenhorn

Joined: Aug 07, 2012
Posts: 26

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

example

refer api Documentation for more method details...,
java.lang.String


-Đinê$h
Adnan Alvee
Greenhorn

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
Greenhorn

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help me with my 1st Java Assignment