0

My issue is wanting to replace the content of a String with another String. For example, if I have the word Generating Code.., then after waiting two seconds, I want the program to replace Generating Code with Generating Code Successful.

* Note that I am using Eclipse, and the following code works for Notepad *

String code = "Generating code...\r";

String successful = code.replaceAll(code, "Generating the code has been successful.\r");

[...]

try {
       System.out.print(code); // prints out Generating Code
       Thread.sleep(2000); // Waits two seconds
       System.out.print(successful); // Replaces Generating Code with has been successful
    }

This code works in Notepad because the \r replaces the text. However, in Eclipse \r does not replace the text, and instead ends the line. Is there a way of getting this functionality to work in Eclipse?

My code in Eclipse is identical to that listed here that works in Notepad++.

Baleroc
  • 167
  • 4
  • 15
  • `\r` doesn't replace the text, it is a carriage return. This looks like a difference in how the two consoles handle carriage returns. – Andy Turner Jun 26 '15 at 16:11
  • https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 – Andy Turner Jun 26 '15 at 16:12
  • Would you know the equivalent of \r functionality in Notepad++ for Eclipse? – Baleroc Jun 26 '15 at 16:13
  • Try googling for "eclipse console carriage return". – Andy Turner Jun 26 '15 at 16:14
  • So apparently there is a bug in Eclipse that prevents this from happening. However, the article you submitted suggested that this was fixed, so I assumed it could have an alternative way. – Baleroc Jun 26 '15 at 16:16
  • The last few (quite recent) comments describe a regression. – Andy Turner Jun 26 '15 at 16:17
  • You want `replace`. `replaceAll` takes a regular expression. Also, use [`System.lineSeparator()`](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#lineSeparator%28%29) instead of `'\r'`. – Elliott Frisch Jun 26 '15 at 16:18
  • How about a C++ equivalent, is it possible to use system("cls"); to clear the screen in Java Eclipse and call the methods again. – Baleroc Jun 26 '15 at 16:23
  • @ElliottFrisch He doesn't want the line separator. He explicitly wants `\r` to move the curser back to the start of the same line the write something else (in this case *"Generating the code has been successful."*). – Tom Jun 26 '15 at 16:25
  • @tom carriage return doesn't do that on any tty I'm familiar with. – Elliott Frisch Jun 26 '15 at 16:26
  • @ElliottFrisch Since I never used that (sounds like a big code smell to me :D), I can't tell on which tty that works, but this is what OP wants :D. He wants to replace a printed line with another one. – Tom Jun 26 '15 at 16:29
  • @tom I guess he wants a java curses library. – Elliott Frisch Jun 26 '15 at 16:30
  • Just out of curiosity, why do you want such a functionality to work in eclipse console??? Normally, you and your developers will be the only people using eclipse to run the code... No??? – Codebender Jun 26 '15 at 16:33
  • @ElliottFrisch Then this question might help: [What's a good Java, curses-like, library for terminal applications?](http://stackoverflow.com/q/439799) (too bad it is off-topic and eligible for closing) – Tom Jun 26 '15 at 16:35

0 Answers0