1

I'm working on a Chat Client and it works all fine, but if you open the Chat Client you see a bar in the top of my frame, and if you enter your name there and you send a message it will show your name + message. But I don't want people to use certain words or characters, and I have no idea how to do that. I tried to look on the internet but couldn't find anything usefull. so this is my code of my Chat Client, also I want to send messages when you press enter but what I did, doesn't work:

    import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

class Chat implements KeyListener{

    JTextArea incoming;
    JTextField output, name;
    JButton send;
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;

    public static void main(String [] args){
        Chat c = new Chat();
        c.Start();
    }
    public void Start(){
        JFrame frame = new JFrame("Chat Client Alpha 1.1");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(700,700);
        frame.setResizable(false);
        frame.setBackground(Color.black);

        JPanel center = new JPanel();
        JPanel south = new JPanel();
        JPanel north = new JPanel();

        JLabel EName = new JLabel("Enter your name: ");
        incoming = new JTextArea(35,45);
        send = new JButton("SEND");
        send.addActionListener(new SendMessageListener());
        output = new JTextField(40);
        name = new JTextField(37);

        name.setText("Default");

        incoming.setEditable(false);
        incoming.setLineWrap(true);

        JScrollPane sc = new JScrollPane(incoming);
        sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        north.add(EName);
        north.add(name);
        center.add(sc);
        south.add(output);
        south.add(send);

        north.setBackground(Color.gray);
        center.setBackground(Color.gray);
        south.setBackground(Color.gray);

        setUpNetworking();

        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        frame.getContentPane().add(BorderLayout.NORTH, north);
        frame.getContentPane().add(BorderLayout.CENTER, center);
        frame.getContentPane().add(BorderLayout.SOUTH, south);

        frame.setVisible(true);
        } private void setUpNetworking(){
            try{
                sock = new Socket("89.99.7.141", 5000);
                InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(streamReader);
                writer = new PrintWriter(sock.getOutputStream());
                incoming.append("Networking Established" + "\n");
            }catch(IOException ex){
                ex.printStackTrace();
                incoming.append("Connecting Failed" + "\n");
            }
        }
        class SendMessageListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                try{
                    writer.println("(" + name.getText() + ")" + " " + output.getText());
                    writer.flush();
                }catch(Exception ex){
                    ex.printStackTrace();
                }
                output.setText("");
                output.requestFocus();
            }
        }
        public class IncomingReader implements Runnable{
            public void run(){
                String message;
                try{
                    while((message = reader.readLine()) !=null){
                        System.out.println("read" + message);
                        incoming.append(message + "\n");
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();

                    }
                }
            }public void keyTyped(KeyEvent e){
                if (e.getKeyCode() == KeyEvent.VK_ENTER){
                    new SendMessageListener();
                    System.out.println("key typed");
                }
            }

            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER){
                    new SendMessageListener();
                    System.out.println("key pressed");
                }
            }

            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER){
                    new SendMessageListener();
                    System.out.println("key released");
                }

            }
        }

Hopefully you understand what I mean. But what I mean with locking words/characters is that people can not use the word "admin" for example except for me.

Sorry for the bad English, I come from Holland.

kees mees
  • 11
  • 1
  • 1
  • 7
  • I would say you need to test the message before you send it to the server. I.E. http://stackoverflow.com/questions/8992100/test-if-a-string-contains-any-of-the-strings-from-an-array shows a convient way to do this. – Craig Smith Jan 02 '15 at 14:02
  • Regular expressions or a blacklist of forbidden words should do it. – duffymo Jan 02 '15 at 14:05
  • I don't really understand what you mean with testing the message? – kees mees Jan 02 '15 at 14:05

1 Answers1

0

You can use a Set containing bad words and then just test whether the typed word is present in set E.g.(hello is the bad word in example below).

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

class Chat implements KeyListener{
JTextArea incoming;
JTextField output, name;
JButton send;
BufferedReader reader;
PrintWriter writer;
Socket sock;

Set<String> badWords = new HashSet<>(Arrays.asList("hello"));


public static void main(String [] args){
    Chat c = new Chat();
    c.Start();
}
public void Start(){
    JFrame frame = new JFrame("Chat Client Alpha 1.1");
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(700,700);
    frame.setResizable(false);
    frame.setBackground(Color.black);

    JPanel center = new JPanel();
    JPanel south = new JPanel();
    JPanel north = new JPanel();

    JLabel EName = new JLabel("Enter your name: ");
    incoming = new JTextArea(35,45);
    send = new JButton("SEND");
    send.addActionListener(new SendMessageListener());
    output = new JTextField(40);
    name = new JTextField(37);

    name.setText("Default");

    incoming.setEditable(false);
    incoming.setLineWrap(true);

    JScrollPane sc = new JScrollPane(incoming);
    sc.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    north.add(EName);
    north.add(name);
    center.add(sc);
    south.add(output);
    south.add(send);

    north.setBackground(Color.gray);
    center.setBackground(Color.gray);
    south.setBackground(Color.gray);

    setUpNetworking();

    Thread readerThread = new Thread(new IncomingReader());
    readerThread.start();

    frame.getContentPane().add(BorderLayout.NORTH, north);
    frame.getContentPane().add(BorderLayout.CENTER, center);
    frame.getContentPane().add(BorderLayout.SOUTH, south);

    frame.setVisible(true);
    } private void setUpNetworking(){
        try{
            sock = new Socket("89.99.7.141", 5000);
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);
            writer = new PrintWriter(sock.getOutputStream());
            incoming.append("Networking Established" + "\n");
        }catch(IOException ex){
            ex.printStackTrace();
            incoming.append("Connecting Failed" + "\n");
        }
    }
    class SendMessageListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            try {
                if (badWords.contains(output.getText())) {
                    writer.println("(" + name.getText() + ")" + " " + "sorry bad words are not allowed");
                } else if (output.getText().length() > 0){
                    writer.println("(" + name.getText() + ")" + " " + output.getText());
                }
                writer.flush();
            }catch(Exception ex){
                ex.printStackTrace();
            }
            output.setText("");
            output.requestFocus();
        }
    }
    public class IncomingReader implements Runnable{
        public void run(){
            String message;
            try{
                while((message = reader.readLine()) !=null){
                    System.out.println("read" + message);
                    incoming.append(message + "\n");
                    }
                }catch(Exception ex){
                    ex.printStackTrace();

                }
            }
        }public void keyTyped(KeyEvent e){
            if (e.getKeyCode() == KeyEvent.VK_ENTER){
                new SendMessageListener();
                System.out.println("key typed");
            }
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER){
                new SendMessageListener();
                System.out.println("key pressed");
            }
        }

        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER){
                new SendMessageListener();
                System.out.println("key released");
            }

        }
    }
sol4me
  • 15,233
  • 5
  • 34
  • 34
  • @keesmees Glad it helps. You can always mark it as answer if it answered your question :) – sol4me Jan 02 '15 at 14:15
  • lol I'm such a noob at this site :P how do I mark your answer as answered? nvm I think I found it – kees mees Jan 02 '15 at 14:16
  • could you help me with another thing? cause I don't want to send a message when the user doesn't type anything so the output length is 0, but it keeps adding the name in front of it so its (name) and then nothing. – kees mees Jan 02 '15 at 14:25
  • @keesmees answer updated b/w If i were you i will use StringUtils from apache commons to check if String is not blank – sol4me Jan 02 '15 at 14:30
  • How do I do that? I'm a beginner :3 so...and can we talk on skype cause I have to avoid extended discussions? – kees mees Jan 02 '15 at 14:33
  • @keesmees You need to add the apache-commons lang dependency(if you are using maven) or download the jar and after that use for e.g. `StringUtils.isNotBlank`. For more info google `apache commons lang stringutils example` – sol4me Jan 02 '15 at 14:41