Hi Everybody,
I was wondering if anybody could help me. I want to find the most frequent lettter, and replace it with another letter. It hen want to find the second most common letter and replace it with another letter. It's not working. Could anybody please help??
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class cracker extends JFrame implements ActionListener{
JLabel lbl = new JLabel("Code Cracker v. 1.0");
JLabel info = new JLabel("Enter a line of code below.");
JTextArea input = new JTextArea(1,15);
JButton go = new JButton("Figure out the code");
JTextArea area = new JTextArea(15,20);
JScrollPane area2 = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public int q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m,
Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M,
zero,one,two,three,four,five,six,seven,eight,nine,ten;
String in = "";
Font cf = new Font("Arial",Font.BOLD,30);
public cracker(){
super( "Code Cracker" );
setSize( 300 , 400 );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible( true );
Container content = getContentPane();
FlowLayout lay = new FlowLayout();
content.setLayout( lay );
content.add( lbl );
lbl.setFont(cf);
content.add( info );
info.setForeground(Color.BLUE);
content.add( input );
input.setBackground(Color.CYAN);
content.add( go);
go.setBackground(Color.ORANGE);
content.add( area2 );
area.setBackground(Color.DARK_GRAY);
area.setForeground(Color.WHITE);
area.setEditable(false);
content.setBackground(Color.WHITE);
go.addActionListener(this);
setContentPane(content);
}
public void actionPerformed(ActionEvent event){
if( event.getSource() == go ){
in = input.getText();
doJob();
}
}
public void doJob(){
q = countChars(input.getText(), 'q');
w = countChars(input.getText(), 'w');
e = countChars(input.getText(), 'e');
r = countChars(input.getText(), 'r');
t = countChars(input.getText(), 't');
y = countChars(input.getText(), 'y');
u = countChars(input.getText(), 'u');
i = countChars(input.getText(), 'i');
o = countChars(input.getText(), 'o');
p = countChars(input.getText(), 'p');
a = countChars(input.getText(), 'a');
s = countChars(input.getText(), 's');
d = countChars(input.getText(), 'd');
f = countChars(input.getText(), 'f');
g = countChars(input.getText(), 'g');
h = countChars(input.getText(), 'h');
j = countChars(input.getText(), 'j');
k = countChars(input.getText(), 'k');
l = countChars(input.getText(), 'l');
z = countChars(input.getText(), 'z');
x = countChars(input.getText(), 'x');
c = countChars(input.getText(), 'c');
v = countChars(input.getText(), 'v');
b = countChars(input.getText(), 'b');
n = countChars(input.getText(), 'n');
m = countChars(input.getText(), 'm');
Q = countChars(input.getText(), 'Q');
W = countChars(input.getText(), 'W');
E = countChars(input.getText(), 'E');
R = countChars(input.getText(), 'R');
T = countChars(input.getText(), 'T');
Y = countChars(input.getText(), 'Y');
U = countChars(input.getText(), 'U');
I = countChars(input.getText(), 'I');
O = countChars(input.getText(), 'O');
P = countChars(input.getText(), 'P');
A = countChars(input.getText(), 'A');
S = countChars(input.getText(), 'S');
D = countChars(input.getText(), 'D');
F = countChars(input.getText(), 'F');
G = countChars(input.getText(), 'G');
H = countChars(input.getText(), 'H');
J = countChars(input.getText(), 'J');
K = countChars(input.getText(), 'K');
L = countChars(input.getText(), 'L');
Z = countChars(input.getText(), 'Z');
X = countChars(input.getText(), 'X');
C = countChars(input.getText(), 'C');
V = countChars(input.getText(), 'V');
B = countChars(input.getText(), 'B');
N = countChars(input.getText(), 'N');
M = countChars(input.getText(), 'M');
one = countChars(input.getText(), '1');
two = countChars(input.getText(), '2');
three = countChars(input.getText(), '3');
four = countChars(input.getText(), '4');
five = countChars(input.getText(), '5');
six = countChars(input.getText(), '6');
seven = countChars(input.getText(), '7');
eight = countChars(input.getText(), '8');
nine = countChars(input.getText(), '9');
zero = countChars(input.getText(), '0');
largest();
}
public int countChars( String str, char searchChar ) {
// Count the number of times searchChar occurs in
// str and return the result.
int i; // A position in the string, str.
char ch; // A character in the string.
int count; // Number of times searchChar has been found in str.
count = 0;
for ( i = 0; i < str.length(); i++ ) {
ch = str.charAt(i); // Get the i-th character in str.
if ( ch == searchChar )
count++;
}
return count;
}
public void largest(){
if( q > w && q > e && q > w && q > r && q > t && q > y && q > u && q > i &&
q > o && q > p && q > a && q > s && q > d && q > f && q > g && q > h &&
q > j && q > k && q > l && q > z && q > x && q > c && q > v && q > b &&
q > n && q > m && q > Q && q > W && q > E && q > R && q > T && q > Y &&
q > U && q > I && q > O && q > P && q > A && q > S && q > D && q > F &&
q > G && q > H && q > J && q > K && q > L && q > Z && q > X && q > C &&
q > C && q > V && q > B && q > N && q > M && q > one && q > two && q > three &&
q > four && q > five && q > six && q > seven && q > eight && q > nine &&
q > zero){
in = in.replace('q','e');
in+="\n\n"+
"* e * = q";
area.setText(in);
}
if (e > w && e > q && e > w && e > r && e > t && e > y && e > u && e > i &&
e > o && e > p && e > a && e > s && e > d && e > f && e > g && e > h &&
e > j && e > k && e > l && e > z && e > x && e > c && e > v && e > b &&
e > n && e > m && e > Q && e > W && e > E && e > R && e > T && e > Y &&
e > U && e > I && e > O && e > P && e > A && e > S && e > D && e > F &&
e > G && e > H && e > J && e > K && e > L && e > Z && e > X && e > C &&
e > C && e > V && e > B && e > N && e > M && e > one && e > two &&
e > three &&
e > four && e > five && e > six && e > seven && e > eight && e > nine &&
e > zero) {
in = in.replace('q', 'e');
in += "\n\n" +
"* e * = q";
area.setText(in);
}
if( r > w && r > e && r > w && r > g && r > t && r > y && r > u && r > i &&
r > o && r > p && r > a && r > s && r > d && r > f && r > g && r > h &&
r > j && r > k && r > l && r > z && r > x && r > c && r > v && r > b &&
r > n && r > m && r > Q && r > W && r > E && r > R && r > T && r > Y &&
r > U && r > I && r > O && r > P && r > A && r > S && r > D && r > F &&
r > G && r > H && r > J && r > K && r > L && r > Z && r > X && r > C &&
r > C && r > V && r > B && r > N && r > M && r > one && r > two && r > three &&
r > four && r > five && r > six && r > seven && r > eight && r > nine &&
r > zero){
in = in.replace('r','e');
in+="\n\n"+
"* e * = r";
area.setText(in);
}
if (t > w && t > e && t > w && t > r && t > t && t > y && t > u && t > i &&
t > o && t > p && t > a && t > s && t > d && t > f && t > g && t > h &&
t > j && t > k && t > l && t > z && t > x && t > c && t > v && t > b &&
t > n && t > m && t > Q && t > W && t > E && t > R && t > T && t > Y &&
t > U && t > I && t > O && t > P && t > A && t > S && t > D && t > F &&
t > G && t > H && t > J && t > K && t > L && t > Z && t > X && t > C &&
t > C && t > V && t > B && t > N && t > M && t > one && t > two &&
t > three &&
t > four && t > five && t > six && t > seven && t > eight && t > nine &&
t > zero) {
in = in.replace('t', 'e');
in += "\n\n" +
"* e * = t";
area.setText(in);
}
}
public static void main (String [] args){
new cracker();
}
}
Thanks in advanced,
C++