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);
}
}
}