1

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;
    }
}
Nilesh
  • 2,089
  • 3
  • 29
  • 53
  • This question is not duplicate of http://stackoverflow.com/questions/7522022/how-to-delete-stuff-printed-to-console-by-system-out-println It do not solve above problem. – Nilesh Jan 13 '15 at 04:53
  • 1
    I believe you can do this with [Java Curses](http://sourceforge.net/projects/javacurses/). Unless you draw the console with `Graphics` you'll need to link with some kind of terminal abstraction. – Elliott Frisch Jan 13 '15 at 04:53

1 Answers1

1

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 );
        }
    }
}
user2570465
  • 2,437
  • 2
  • 18
  • 22
  • TYSM @user2570465. Above method worked for [my case](https://github.com/TheniL/group-chat). But **msg looks Bare (no heading at left)** Okay fine, it looks better if console's last line always prints `you : _`. Later the same message enters the feed... I used `System.out.print("\r");`, still a problem remains, half typed msg vanishes from console but remains in buffer. Can you tell any workaround? – Nilesh Jan 13 '15 at 14:02
  • @niLesh Sorry, I do not understand. Could you post a screenshot and circle the problem? Thanks. – user2570465 Jan 13 '15 at 15:20