Search code examples
javacmdterminalconsoleerase

Erase console text java


I am making a console chat program in java. Which accepts user text input and sends to server, server then broadcast this to all clients. I want to erase the text entered by user, on his console.

I would prefer platform independent solution.

import java.io.*;
class Test
{
    public static void main(String args[])
    {
    System.out.print("you: ");
    String t=getString();
    System.out.println("We accepted : " + t);
    }
    static String getString()
     {
    String s;
    try{
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        s = bufferRead.readLine();
        //int count = 1; 
        //System.out.print(String.format("\033[%dA",count)); // Move up
        //System.out.print("\033[2K");        // Erase line content, works on terminal but not on cmd
        for(int i=0;i<s.length();i++)
            System.out.print("\b");     //dont know, why it doesnt works??
        }   
    catch(IOException e)
        {e.printStackTrace(); s="Error";}
        return s;
    }
}

Solution

  • This is my interpretation of your question:

    I am a client and I type a message, "Hello." Everybody else sees "Hello". However, by typing "Hello" into my console, the message is already there. I don't want to see another "Hello" appear on my screen from the announcer.

    Is that the functionality you are trying to achieve? If so, I would suggest that you do not erase the message. If you've printed something to console, it might not be so easy to erase it. Rather you could just never send it in the first place.

    The following is what I have generally used for messaging systems in which you don't want the sender to see his message twice:

    Conveniently, you have a member object for each person connected to the server, and each member has a name. Hopefully names will be unique. Now, if you ensure that every member gets identified with a name upon connecting to your server, then when you announce the message to everyone, you just make sure that you don't announce it to the sender.

    Specifically, I would modify the constructor and the run() method of the announcer class:

    String message;
    String senderName;
    
    announcer( member x , String msg ) {
        senderName = x.name;
        message= x.name + " : " + msg ;
    }
    
    public void run() {
        for ( int i=0 ; i<server.group.size() ; i++ ) {
            if ( !server.group.get( i ).name.equals( senderName ) ) {
                send( server.group.get( i ).sck );
            }
        }
    }
    

    Now, the message is sent to everyone except the sender, which achieves the equivalent result of sending it to the sender and then erasing it. I'm sorry if you will have to write more code to get everyone's name set up correctly, but I haven't heard of any implementations where duplicate messages were just "erased" from standard output.

    Alternatively, if your server.group objects are the same as the member objects you pass into the announcer constructor, then this will work

    String message;
    member sender;
    
    announcer( member x , String msg ) {
        sender = x;
        message= x.name + " : " + msg ;
    }
    
    public void run() {
        for ( int i=0 ; i<server.group.size() ; i++ ) {
            if ( server.group.get( i ) != sender ) {
                send( server.group.get( i ).sck );
            }
        }
    }