Arrange thread message

0

Good evening.

I need your help in something simple, I'm doing a chat via RMI, the implementation of the program is already working more as I want, but I'm breaking your head in a simple print. I need to organize the output so it looks like this:

Chat: enter message

so-and-so says: typed message

My program is a thread for reading the messages from the server, and it's just these messages that I'm not able to organize.

The messages are currently coming out as follows:

Chat: Typing Message

Chat: so-and-so says: typed message

That is, I would like to follow the sequence

Message

Tablet

I know it's a bit of a beast to doubt, but I really can not do it kkkk follow my code:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Scanner;

public class ChatCliente{

private static Scanner scanner;

public static void main(String[] args) {
    try {

        ChatIF chat = (ChatIF) Naming.lookup("rmi://localhost:1098/Chat"); //Buscando a Interface no Servidor

        String nome;
        String msg = "";
        scanner = new Scanner(System.in);
        int cont = chat.getmensagens().size();

        //Recebendo o nome do Usuario
        System.out.println("---------------------------------------------");
        System.out.println("       SEJA BEM VINDO AO CHAT                ");
        System.out.println("---------------------------------------------");
        System.out.print("[Chat] Por Favor, Digite seu nome: ");
        nome = scanner.nextLine();

        //Thread responsavel por ler as mensagens do servidor
        Thread thread = new Thread(new Runnable() {
            int cont = chat.getmensagens().size();

            @Override
            public void run() {
                try {
                    while(true){
                        if (chat.getmensagens().size() > cont){


   System.out.println(chat.getmensagens().get(chat.getmensagens().size()-1));
                            cont++;
                        }
                    }
                }catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        System.out.println("---------------------------------------------");
        System.out.println("Ola, " + nome + "! Voce agora esta conectado!");
        System.out.println("---------------------------------------------");

        thread.start();

        //Envia as mensagens  para o servidor
        while(true){
            System.out.print("[Chat]"+": ");
            msg = scanner.nextLine();           
            chat.setMensagem(nome+" diz"+": "+msg);
        }


    } catch (Exception e) {
        System.out.println("Ocorreu um erro: " + e);
    }
}
}
    
asked by anonymous 14.02.2018 / 07:19

1 answer

0

The problem is that the two threads are printing on the same console.

At this point you print the chat string without breaking the line.

while(true){
    System.out.print("[Chat]"+": "); // Aqui você imprime o "[Chat]:" sem quebra de linhas
    msg = scanner.nextLine();           
    chat.setMensagem(nome+" diz"+": "+msg);
}

At this point, you print the message in the same console as the previous message.

System.out.println(chat.getmensagens().get(chat.getmensagens().size()-1));

So you're seeing: "[Chat]: message". Thread messages are mixed. To be organized you need to put the threads into different classes and create a main for each one. Then you need to run both main. This way you will have two consoles.

    
19.02.2018 / 16:44