• Post Reply Bookmark Topic Watch Topic
  • New Topic

periodically display diff string in the same position in the command console  RSS feed

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all:

in my program, i want to PERIODICALLY display some string in the SAME POSITION in the windows COMMAND CONSOLE.
certainly, it is easy to solve the "periodically" problem by using the Timer class, but what hamper me is how to display them in the SAME position within the windows command consle. so let us make the complex request simpler, consider following situation in your code:

System.out.print("hello world");
System.out.print("good morning");

the output may be following:

hello worldgood morning

but what i want is following:

1 display "hello world" in the console
2 delete "hello world" in the console
3 dispaly "good morning" in the original position of "hello world"?


who can help me?
thanks in advance!!
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just run the bellow code. Analyze the code and the output both.

 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extension of Chetan Parekh example.


import java.util.Date;
public class Clock {
public static void main(String args[]) {
System.out.print(new Date());
while(true){
try{
Thread.sleep(1000);
System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
System.out.print(new Date());
}catch(Exception e){
e.printStackTrace();

}
}
}
}
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the ASCII character for carriage return and you can just overwrite the previous output rather than printing a bunch of backspaces.

[ February 17, 2006: Message edited by: Joe Ess ]
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess you have maid a nice point.
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Chetan. I learned that little trick here at the Java Ranch. It's amazing what you can pick up just hanging around.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!