Java simple assignment help!! beginner
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.
Re: Java simple assignment help!! beginner
What have you written so far? We won't really write code for you, but will guide and help with errors.
Re: Java simple assignment help!! beginner
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";
}
}
Re: Java simple assignment help!! beginner
A substring would work. Have you looked at the javadoc for substring? Keep in mind there are two overloaded versions. Think about how you would go through a string and pick out words. You would need some form of a loop.
I should probably mention that Java has some nice built in utilities that will make this a lot easier. It looks like you are just starting out, so I am assuming that the book wants you to think of your own way of solving it without using Java's implementation. If this is an assignment from a book, then they probably discussed String manipulation in the chapter and maybe they do want you to use the String methods.
Re: Java simple assignment help!! beginner
what about:
...code removed
Re: Java simple assignment help!! beginner
@FlyNn, please be cognizant of the difference between helping someone do their work vs giving them the answer. One of the hardest parts of programming is problem solving, and being spoonfed an answer does nothing to help one tune these essential skills.