Results 1 to 8 of 8
  1. #1
    fastcar83 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default I need help doing a small program.

    Hi guys, I'm new here. I need some help doing a program and I wondered if any of you likes a tiny challenge! I need to design a program that does the following:
    1. Reads a whole phrase ending with the word "fi" (it's in catalan).
    2. It then needs to read 2 seperate letters.
    3. Change all of the letters equal to the first one, to the second one.
    4. Print phrase.

    Example:

    In: "My name is not Jeremy fi" e u
    Out: "My namu is not Jurumy fi

    Can someone lend me a hand please? thnx!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Is this a homework assignment? Do you have restrictions on what functions you can and cannot use? There is a replaceAll() method in Java that could be quite handy, but I'm not sure if you're supposed to design your own method to do this.

    Either way, have you done input before? You will want to read up on Scanners and input in general. That way you can accept the statement + letters inputted. Then, you can use println() to output the new sentence.

  3. #3
    fastcar83 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    It IS homework and I've been told that I can't use any character replacement method. And yes I have already worked with scanners and, of course, I'm familiar with System.out.println...BUT I don't know what to do about the replacing thing. Can you help me please?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You'll want to start by creating a method that accepts three strings--one sentence, one character to replace, and one to replace it with.

    Next, you want to create a blank string, then loop through the existing sentence from start to finish (using a for loop and charAt()), and add the character to the blank string if it's NOT equal to the one you want to replace, or add the replaced character if it IS.

    Does that make sense? (I have to head to class for a bit but I'll check back in a couple hours to see how you're making out.)

  5. #5
    juamd is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    Hi !!!

    you can:

    1. Use length to determine what is the string's size.
    2. Read the string inside a loop and add the element to array but changing the characters you need to change.
    3. Read the array inside a looop and create a new string.
    4. Show the string.

    Hope be useful.

  6. #6
    fastcar83 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I'm going to try and use indexOf to find all of the positions and replace them. I'll reply back!

  7. #7
    fastcar83 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Zack I ended up doing what you said and it ended up being really short and neat so thnx for the help! :)

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're welcome! If you had used indexOf you would have issues with substring() and such that would have been messy. Glad you have it working!

Similar Threads

  1. Small calender program
    By Moustafa taha in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 07:52 PM
  2. Small yahtzee program
    By kimmelim in forum New To Java
    Replies: 20
    Last Post: 03-12-2009, 12:11 PM
  3. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 01:01 AM
  4. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 02:29 AM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •