Anagram Checker Applet Source Code

Anagrammy Awards > Anagram Checker Applet > Source Code


This applet consists of two parts, one that sets out the design of the applet (on the left)and one that does the actual checking (on the right). Naturally enough, the two are anagrams of each other (letters only; numbers and symbols proved to be impossible).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JGrammzApplet extends JApplet {

  private JTextArea oh, ag, rs;
  private String orig = "";
  private String gram = "";

  public void init () {
    Container fx = getContentPane();
    oh = new JTextArea(4,25);
    oh.setEditable(true);
    oh.setLineWrap(true);
    ag = new JTextArea(4,25);
    ag.setEditable(true);
    ag.setLineWrap(true);
    rs = new JTextArea(4,25);
    rs.setEditable(false);
    rs.setLineWrap(true);
    JButton tex = new JButton("Go Check");
    tex.addActionListener(new ActionListener () {
      public void actionPerformed(ActionEvent e) {
        if (oh.getText()!=orig) {
          orig = oh.getText();
          rs.setText(Chequer.findThisGram(orig, gram));
        }
        if (ag.getText()!=gram) {
          gram = ag.getText();
          rs.setText(Chequer.findThisGram(orig, gram));
        }
      }
    });
    JButton clearAll = new JButton("Go clear all");
    clearAll.addActionListener(new ActionListener () {
      public void actionPerformed(ActionEvent e) {
        orig = "";
        gram = "";
        oh.setText("");
        ag.setText("");
        rs.setText("");
      }
    });
    Font middF = new Font("SansSerif", 1, 20);
    JLabel cff = new JLabel("=");
    cff.setFont(middF);
    tex.setFont(middF);
    JPanel pO = new JPanel();
    JPanel pR = new JPanel();
    pO.setBounds(0,0,600,80);
    pR.setBounds(150,80,300,200);
    pO.add(new JScrollPane(oh),BorderLayout.CENTER);
    pO.add(cff);
    pO.add(new JScrollPane(ag), BorderLayout.CENTER);
    pR.add(tex);
    pR.add(new JScrollPane(rs), BorderLayout.CENTER);
    pR.add(clearAll);
    fx.setLayout(null);
    fx.add(pO);
    fx.add(pR);
  }
}

public class Chequer {

  public static String findThisGram (String MJOriginal,
        String UBGram) {
    String dataProceed = "";
    String [] netText = new String [2];
    char [] alphLetter = {'A','B','C','D','E','F','G','H','I',
        'J','K','L','M','N','O','P','Q','R','S','T','U','V',
        'W','X','Y','Z'};
    netText[0] = MJOriginal;
    netText[1] = UBGram;
    String [] upperCaseTextP = new String [2];
    int [][] RLetter = new int [2][26];
    int myLengthV = 0;
    int [][] noAnag = new int [2][26];
    for (int vow=0; vow<2; vow++) {
      upperCaseTextP[vow] = netText[vow].toUpperCase();
      myLengthV = upperCaseTextP[vow].length();
      for (int bdf=0; bdf<myLengthV; bdf++) {
        for (int joe=0; joe<=25; joe++) {
          if (upperCaseTextP[vow].charAt(bdf)==alphLetter[joe])
            RLetter[vow][joe]++;
        }
      }
    }
    int bullet = 0;
    boolean XMissLetters = false;
    boolean XSLetters = false;
    boolean noGram = false;
    for (int ij=0; ij<=25; ij++) {
      bullet = RLetter[0][ij]-RLetter[1][ij];
      if (bullet>0) {
        noAnag[0][ij] = bullet;
        noGram = true;
        XMissLetters = true;
      } else if (bullet<0) {
        noAnag[1][ij] = -bullet;
        noGram = true;
        XSLetters = true;
      }
    }
    if (noGram) {
      if (XSLetters) {
        dataProceed = dataProceed+"Excess letters: ";
        for (int jx=0; jx<=25; jx++) {
          if (noAnag[1][jx]!=0) {
            if (noAnag[1][jx]!=1)
              dataProceed = dataProceed+noAnag[1][jx];
            dataProceed = dataProceed+alphLetter[jx]+" ";
          }
        }
        dataProceed = dataProceed+"\n";
      }
      if (XMissLetters) {
        dataProceed = dataProceed+"Missing letters: ";
        for (int w=0; w<=25; w++) {
          if (noAnag[0][w]!=0) {
            if (noAnag[0][w]!=1)
              dataProceed = dataProceed+noAnag[0][w];
            dataProceed = dataProceed+alphLetter[w]+" ";
          }
        }
      }
    } else {
      dataProceed = dataProceed+"It's an anagram!";
    }
    return dataProceed;
  }
}


Home

 | The Anagrammy Awards | Enter the Forum | Facebook | The Team

Information

 | Awards Rules | Forum FAQ | Anagrams FAQ | History | Articles

Resources

 | Anagram Artist Software | Generators | On-line | Books | Websites

Archives

 | Winners | Nominations | Hall of Fame | Anagrammasia | Literary | Specials

Competition

 | Vote | Current Nominations | Leader Board | Latest Results | Old Results | Rankings

Miscellaneous

 | Tribute Page | Records | Sitemap | Search | Anagram Checker | Email Us | Donate

Anagrammy Awards

  © 1998-2024