Hi I have a project where when you input text into a textarea if a certain word is put in I need to change this word I'm having real trouble with this Here Is what I have so far but it will not reconize some of the words

/*


import javax.swing.*;
import java.util.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.GroupLayout.*;


public class quickSort extends JFrame
        implements DocumentListener {

    private JLabel jLabel1;
    private JScrollPane jScrollPane1;
    private JTextArea textArea;

 
    private final List<String> words;
 



    public quickSort() {
        super("TextAreaDemo");
        initComponents();

        textArea.getDocument().addDocumentListener(this);



        words = new ArrayList<String>(4);
        words.add("shit");
        words.add("swine");
        words.add("double drat");
        words.add("drat");
    }


    private void initComponents() {
        jLabel1 = new JLabel("shit looser swine spectacular swing");

        textArea = new JTextArea();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        textArea.setColumns(20);
        textArea.setLineWrap(true);
        textArea.setRows(5);
        textArea.setWrapStyleWord(true);

        jScrollPane1 = new JScrollPane(textArea);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        //Create a parallel group for the horizontal axis
        ParallelGroup hGroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
        //Create a sequential and a parallel groups
	SequentialGroup h1 = layout.createSequentialGroup();
        ParallelGroup h2 = layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
        //Add a scroll panel and a label to the parallel group h2
	h2.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE);
        h2.addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE);

	//Add a container gap to the sequential group h1
	h1.addContainerGap();
        // Add the group h2 to the group h1
	h1.addGroup(h2);
        h1.addContainerGap();
        //Add the group h1 to hGroup
	hGroup.addGroup(Alignment.TRAILING,h1);
        //Create the horizontal group
	layout.setHorizontalGroup(hGroup);

	//Create a parallel group for the vertical axis
        ParallelGroup vGroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
        //Create a sequential group
	SequentialGroup v1 = layout.createSequentialGroup();
        //Add a container gap to the sequential group v1
	v1.addContainerGap();
        //Add a label to the sequential group v1
	v1.addComponent(jLabel1);
        v1.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        //Add scroll panel to the sequential group v1
	v1.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE);
        v1.addContainerGap();
        //Add the group v1 to vGroup
	vGroup.addGroup(v1);
        //Create the vertical group
	layout.setVerticalGroup(vGroup);
        pack();

    }
    // Listener methods

    public void changedUpdate(DocumentEvent ev) {
    }

    public void removeUpdate(DocumentEvent ev) {
    }

    public void insertUpdate(DocumentEvent ev) {
        if (ev.getLength() != 1) {
            return;
        }

        int pos = ev.getOffset();
        String content = null;
        try {
            content = textArea.getText(0, pos + 1);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        // Find where the word starts
        int w;
        for (w = pos; w >= 0; w--) {
            if (! Character.isLetter(content.charAt(w))) {
                break;
            }
        }
        if (pos - w < 5) {
            // Too few chars
            return;
        }

        String prefix = content.substring(w+1,pos).toLowerCase();

        int n = Collections.binarySearch(words, prefix);

        if (n < 0 && -n <= words.size()) {
            String match = words.get(-n - 1);

             if (match.equals("swine")) {
                   System.out.print("swine was a match\n");

            }
             if (match.equals("shit")) {
                   System.out.print("shit was a match\n");

            }
            if (match.equals("double drat")) {
                   System.out.print("double drat was a match\n");
               
            }
        if (match.equals("drat")) {
                   System.out.print("drat was a match\n");

            }
    }
    }
 public void out(String ev,int position) {

     if(ev.toLowerCase().equals("swin")){
          System.out.print("s!7y ");
           
                 }else
      System.out.print("SI\t");
     
    }


   

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                new quickSort().setVisible(true);
            }
        });
    }


}

can anyone help me please?

Recommended Answers

All 2 Replies

Are you trying to manually do a character by character search to match words? That is a mistake. Technically it could work but the amount of effort would be enormous and fruitless. I see that your "words" can actually be multiple words with spaces between them. My first thought is to create a regular expression for each "bad word" and then scan through your input, seeing if that bad word/pattern is in the input. If it is in the input, then you can do something such as *** it out. For learning about regular expressions, see here:

http://java.sun.com/docs/books/tutorial/essential/regex/

If you read for a few minutes you'll see that their examples give you exactly what you need and you don't even need to get your hands dirty to accomplish your task. Matching shit to SHIT or Shit or other variations of lowercase/uppercase is also just as easy.

That perfect thanks very much for your help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.