Results 1 to 4 of 4
  1. #1
    Zitro is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default ---HELP (replace method)---

    I'm writing a program in Eclipse where the user inputs a sentence. After the sentence is inputted, the program is supposed to replace any instances of the letter "o" found in the sentence with "e" and vice versa. But the program is supposed to do both at the same time and that's where I'm stuck.
    import java.util.Scanner;

    public class letterReplacement
    {
    public static void main (String [] args)
    {
    Scanner keyboard = new Scanner(System.in);

    String inputSentence;
    System.out.println("Enter a sentence");
    inputSentence= keyboard.nextLine();
    inputSentence = inputSentence.replace('e','o');
    inputSentence = inputSentence.replace('o','e');

    System.out.println(inputSentence);

    }

    }

    I got the gist of it down and I know why it's not working (the bold part) but idk how to fix it. I can't make the program replace the letters at the same time. It only outputs the second part because it comes after obviously. How do I make it stop and take in the new string?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ---HELP (replace method)---

    Do replace 3 times and use a nonsense String, such as "##", as a placeholder for your first replace.

  3. #3
    Zitro is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: ---HELP (replace method)---

    yeah im not getting it. Would you mind demonstrating the code?

  4. #4
    Zitro is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: ---HELP (replace method)---

    Nvmnd I got it. thanks alot. appreciate it.

Similar Threads

  1. How to replace int with asterisk
    By erin.ctm in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:13 AM
  2. Using a replace method...
    By paul in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 05:50 AM
  3. problems with replace method
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 10:11 PM
  4. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 09:14 PM

Tags for this Thread

Posting Permissions

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