-1

I have this jButton that transfers the text from one text area to another but now I need to make it strip the sentence but I don't know how to call on the strip string. Thanks in advance!

Here's the code:

package encryptor;

import java.io.File;
import javax.swing.JFileChooser;
import java.util.Scanner;
import java.awt.event.ActionListener;

public class Encryptor extends javax.swing.JFrame
{  
public Encryptor()
{
    initComponents();
    setLocationRelativeTo(null);
}

private void jButtonEncryptActionPerformed(java.awt.event.ActionEvent evt)                                               
{                                                   
    jTextAreaTarget.setText(jTextAreaSource.getText());
    jButtonEncrypt.addActionListener(new ActionListener());
}                                              

public static String strip(String s)
{
    String target = "";
    char[] a = s.toUpperCase().toCharArray();
    for (int i = 0; i < a.length; i++)
    {
        char c = a[i];
        if (c >= 'A' && c <= 'Z')
        {
            target += c;
        }
    }
    return target;
}
BradleyN
  • 1
  • 1
  • 1
  • 1
    `jTextAreaTarget.setText(strip(jTextAreaSource.getText()))`. I don't want to be rude, but Swing is quite complex, and requires to understand serious OO and threading concepts. If you don't know how to call a method yet, you shouldn't deal with Swing. Practice on basic exercises just printing to System.out. BTW, your event handling code doesn't make sense. This doesn't even compile. – JB Nizet Apr 01 '16 at 22:04
  • You should not mix application code and GUI either. Encryption is not something you mix with swing components, if just to avoid complexity (minimize coupling, to be precise). – Maarten Bodewes Apr 01 '16 at 23:39

1 Answers1

0

If you want to strip everything but alphabets then you can probably use String.replaceAll() with Regex, see the example below:

String s = "dsakufbsa32321ofbs  odsd";
System.out.println(s.replaceAll("[^A-Za-z]", ""));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102