-2

I have problem with this task:

User will type some text.

  1. If text has 'a' triple this letter.
  2. If text has simply 'd' delete this letter.
  3. If text has double 'b' write only one 'b'.

I understand this outlines but I do not know how to do it. How should I search text with charAt? What if I find where it is, where should I write this trippled letter?

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
Tomas
  • 11
  • 1
  • 4
    Have you tried anything so far that you could show us? You should always post some code along with your questions on Stackoverflow – Ben Jan 18 '14 at 10:39
  • What do you want to do with the output? Show it to te user, I suppose? – Noctua Jan 18 '14 at 10:39
  • Try replace method in your String variable. – lummycoder Jan 18 '14 at 10:39
  • I know that should post some code but I do not have any. I really do not know how to do it. Yes I want to show it to the user. I will try with replace.Thank you – Tomas Jan 18 '14 at 10:41
  • I believe first condition should look like (if text has letter 'a' - triple it). Or do you mean any letter? Clarify it. But anyway - this is basics of java, you should read it firstly. – lummycoder Jan 18 '14 at 10:44

1 Answers1

0

It really is not that difficult! Go about it sequentially. First replace all the "a"s as you want them to be replaced, then all the "d"s and then all the "b"s. Here's a simple example with replace():

public static void main(String args[]) {
    System.out.print("Word:");
    Scanner scanner = new Scanner(System.in);
    String foo = scanner.next();
    foo = foo.replace("a", "aaa");
    System.out.println(foo);
    foo = foo.replace("d", "");
    System.out.println(foo);
    foo = foo.replace("bb", "b");
    System.out.println(foo);
}

Let me know if this is what you wanted. Also, you could do this with charAt() and String manipulation as well, but that would be a bit more involved.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41