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++

Recommended Answers

All 7 Replies

First learn proper OO techniques and proper Java class naming. Then maybe I'll take a look.

>learn proper OO techniques
That I can understand.

>proper Java class naming
Sun recommends a style for Java, but that doesn't mean it has to be used. I disagree with several of their recommendations and thus, don't use them. As long as the style is consistent, anyone should be able to follow code with minimal effort. Forcing your style onto others is the height of arrogance.

consistens class and method naming makes interaction between classes from others a lot easier.
And remember that in a professional environment coding standards are crucial and those include naming conventions. As the Sun conventions are logical and (of course) used by the standard API it's best to follow them as just about all the world does it.

I don't agree with all the Microsoft conventions for C(++) either, but when coding for Windows in C++ I do follow them for that very reason.
Same with Borland's standards for Delphi. Those are also open for debate, but following them makes for more transparency.

The greatest arrogance is thinking your ideas are better than those of the majority and following them even when it's in contradiction to commonly used best practice.

>The greatest arrogance is thinking your ideas are better than those of the majority
Yes, and I'm an arrogant, willful, stubborn person. However, this reeks of the idea that the majority is always right, and we know that to be a false statement. :)

>in a professional environment coding standards are crucial
And not all professional environments use the same coding standards. While it's incredibly unlikely that a professional would be writing a class called "cracker", we can't assume otherwise without good cause. It's entirely likely that C++ is being forced to use a coding standard that's "contradictory to common best practice". Your refusal to help based solely on that is somewhat cruel, don't you think?

Hope you guys don't mind me saying anything, but I really like (I think it's the VB.NET naming conventions)...Java from what I read wants you to do this just for a simple button:

startButton;

which in VB.NET, the proper way was this:

btnStart;

I think this is much more logical and simpler...Their idea was to precede it with a three letter representation of the component.

ok, i'm srry for the bad style of Java, but im a newb. I've never been taught in a classroom environment before and I don't even know what Java Conventions are.

Can anybody offer some help... please?

may i suggest something that may make the code more readible usign for loops by putting all that stuff in arrays

This goes through a string and sums up all the letters A-Z, a-z and numbers 0-9 and then displays the count at the end. You could use a simple loop to find the biggest num. I hope this helps.


eg...
System.out.println( (int) 'a'); //this would print 97

or to load all the lower case letters in an array

String someString = "Gerardo Lopez ";		

int[] lowerCaseLetterCount = new int[26];
int[] upperCaseLetterCount = new int[26];
int[] numberCount = new int[10];
char someLetter;

for (int i=0; i<26; i++)
{
   lowerCaseLetterCount[i] = 0;
   upperCaseLetterCount[i] = 0;
}

for (int i=0; i<10; i++)
	numberCount[i] = 0;

for (int i=0; i<someString.length(); i++)
{
   char someChar = someString.charAt(i);

   if ( (int) someChar >= (int) 'a' && (int) someChar <= (int) 'z')
      lowerCaseLetterCount[(int) someChar - (int) 'a']++;
  else if ( (int) someChar >= (int) 'A' && (int) someChar <= (int) 'Z')
      upperCaseLetterCount[(int) someChar - (int) 'A']++;
  else if ( someChar >= 0 && someChar<= 9 )
      numberCount[Integer.parseInt(Character.toString(someChar))]++;
}


for (int i=0; i<26; i++)
{
   someLetter = (char) (i + (int) 'a');
   System.out.println( someLetter +": "+lowerCaseLetterCount[i]);
}
for (int i=0; i<26; i++)
{
   someLetter = (char) (i + (int) 'A');
   System.out.println(someLetter +": "+upperCaseLetterCount[i]);
}
for (int i=0; i<10; i++)
{
	System.out.println(i +": "+numberCount[i]);
}

here is a little something something that I hope helps out

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.