Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help me with my 1st Java Assignment! Stuck!!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help me with my 1st Java Assignment! Stuck!!

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me with my 1st Java Assignment! Stuck!!

    Do you have any specific questions about your assignment?
    Please post your code and any questions about problems you are having.

    Look at the String class and its methods:
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my 1st Java Assignment! Stuck!!

    Quote Originally Posted by Norm View Post
    Do you have any specific questions about your assignment?
    Please post your code and any questions about problems you are having.

    Look at the String class and its methods:
    Java Platform SE 7
    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";

    }
    }

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my 1st Java Assignment! Stuck!!

    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";

    }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me with my 1st Java Assignment! Stuck!!

    Now you need to use a String class method to locate the part of the String (the substring) that you want to replace.

    There are several methods in the String class that will help you. Read the API doc for the String class and try some of the methods. Use the System.out.println() method to print out the results to see what the method does.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Introduction to programing class. I am stuck on my assignment arrays.
    By dozindave in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2012, 10:31 PM
  2. 1st Assignment Sem 2 Counter + GUI
    By BITmixit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 18th, 2012, 08:22 AM
  3. 1st Java class assignment, and having some difficulties. Little help please.
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 27th, 2011, 04:09 AM
  4. [SOLVED] stuck on an assignment and need help with repetition
    By teeej86 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 20th, 2011, 07:03 PM
  5. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM