Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me with my 1st Java Assignment

 
Adnan Alvee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where's your code? What problems are you having with it?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use replaceFirst(String regex,String replacement) method for your problem

example

refer api Documentation for more method details...,
java.lang.String
 
Adnan Alvee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic