-2

I'm currently writing an password solver in java.

I know what static means, but sometimes I'm not able to find the most simple solutions.

I don't want any static variables.

I'm think it is because of the main method in my first class, but I am nit sure.

package Main;

public class Main {

    Main() {
        new CodeSolver();
    }

    public static void main(String[] args) {
        new Main();
    }

}

CodeSolver:

package Main;

import GUI.Frame;

public class CodeSolver {
    String codeOld;
    String code ;

    String[] ID;

    public CodeSolver(){
        RegisterId();
        new Frame(code);
    }

    public String giveCode(){
        return code;
    }

    public void RegisterId(){
        ID = new String[62];

        ID[0] = "a"; ID[1] = "b"; ID[2] = "c"; ID[3] = "d"; ID[4] = "e"; ID[5] = "f"; ID[6] = "g"; ID[7] = "h"; ID[8] = "i"; ID[9] = "j"; ID[10] = "k"; ID[11] = "l"; ID[12] = "m"; ID[13] = "n"; ID[14] = "o"; ID[15] = "p";
        ID[16] = "q"; ID[17] = "r"; ID[18] = "s"; ID[19] = "t"; ID[20] = "u"; ID[21] = "v"; ID[22] = "w"; ID[23] = "x"; ID[24] = "y"; ID[25] = "z"; 
        ID[26] = "A"; ID[27] = "B"; ID[28] = "C"; ID[29] = "D"; ID[30] = "E"; ID[31] = "F"; ID[32] = "G"; ID[33] = "H"; ID[34] = "I"; ID[35] = "J"; ID[36] = "K"; ID[37] = "L"; ID[38] = "M"; ID[39] = "N"; ID[40] = "O"; ID[41] = "P"; 
        ID[42] = "Q"; ID[43] = "R"; ID[44] = "S"; ID[45] = "T"; ID[46] = "U"; ID[47] = "V"; ID[48] = "W"; ID[49] = "X"; ID[50] = "Y"; ID[51] = "Z"; 
        ID[52] = "0"; ID[53] = "1"; ID[54] = "2"; ID[55] = "3"; ID[56] = "4"; ID[57] = "5"; ID[58] = "6"; ID[59] = "7"; ID[60] = "8"; ID[61] = "9";
    }

    public void TestLabel(){
        for(int i=0; i <= ID.length; i++){
            codeOld = code;
            code = ID[i];
            System.out.println(ID[i]);
        }
    }
}

Frame:

package GUI;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Main.CodeSolver;

public class Frame extends JFrame {
    private static final long serialVersionUID = 1L;

    String code01;
    String code;
    JPanel panel;
    JLabel label;

    public Frame(String codeNew){
        code = codeNew;

        this.setTitle("CodeSolver");
        this.setSize(600, 300);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        panel = new JPanel();
        panel.setBounds(0, 0, 600, 300);
        panel.setBackground(Color.BLACK);
        panel.setVisible(true);

        label = new JLabel(code);
        label.setBounds(0, 0, 600, 300);
        label.setForeground(Color.GREEN);

        label.setFont(new Font("Helvetica New", Font.PLAIN, 60));
        label.setVisible(true);

        panel.add(label);

        this.add(panel);
    }

    public void getCode(){
        code01 = CodeSolver.giveCode();
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
LK.
  • 1,781
  • 1
  • 10
  • 5

2 Answers2

3

You can't access a non-static method in a static context:

public void getCode(){
    code01 = CodeSolver.giveCode(); //This is not a static method
}

Either make the giveCode() method static (and all that entails) or create an instance member and call giveCode() on that instance.

callyalater
  • 3,102
  • 8
  • 20
  • 27
  • As I do not know if the method was intended to be static and therefore require a static variable or if he intends to have an instance member variable of type CodeSolver and call the method on that instance, I can't properly give a suggested fix. – callyalater Feb 10 '16 at 16:52
1

The problem is that you need an instance of the CodeSolver because the getCode() method is not static, meaning you can't use the class to access that method.

You should also separate the concerns of the Frame from the CodeSolver, they are seperate objects entirely. A Frame should use the CodeSolver, the CodeSolver should not manage or depend on the Frame.

So, the recommended approach is remove it from the CodeSolver, and create an instance of the CodeSolver in the Frame.

For example, (this displays a black box? not sure where that green label is supposed to be...)

CodeSolver - Add a solve() method that operates on the codeToSolve field and returns the solved code.

public class CodeSolver {
    String[] ID;
    String codeToSolve;

    public CodeSolver(){
        this("");
    }

    public CodeSolver(String codeToSolve) {
        registerId();
        this.codeToSolve = codeToSolve;
    }

    public String solve(){

        String code = "";
        // Do some work on this.codeToSolve

        return code;
    }

    public void registerId(){
        ID = new String[62];

        ID[0] = "a"; ID[1] = "b"; ID[2] = "c"; ID[3] = "d"; ID[4] = "e"; ID[5] = "f"; ID[6] = "g"; ID[7] = "h"; ID[8] = "i"; ID[9] = "j"; ID[10] = "k"; ID[11] = "l"; ID[12] = "m"; ID[13] = "n"; ID[14] = "o"; ID[15] = "p";
        ID[16] = "q"; ID[17] = "r"; ID[18] = "s"; ID[19] = "t"; ID[20] = "u"; ID[21] = "v"; ID[22] = "w"; ID[23] = "x"; ID[24] = "y"; ID[25] = "z";
        ID[26] = "A"; ID[27] = "B"; ID[28] = "C"; ID[29] = "D"; ID[30] = "E"; ID[31] = "F"; ID[32] = "G"; ID[33] = "H"; ID[34] = "I"; ID[35] = "J"; ID[36] = "K"; ID[37] = "L"; ID[38] = "M"; ID[39] = "N"; ID[40] = "O"; ID[41] = "P";
        ID[42] = "Q"; ID[43] = "R"; ID[44] = "S"; ID[45] = "T"; ID[46] = "U"; ID[47] = "V"; ID[48] = "W"; ID[49] = "X"; ID[50] = "Y"; ID[51] = "Z";
        ID[52] = "0"; ID[53] = "1"; ID[54] = "2"; ID[55] = "3"; ID[56] = "4"; ID[57] = "5"; ID[58] = "6"; ID[59] = "7"; ID[60] = "8"; ID[61] = "9";
    }

    public void testLabel(){
        for (int i = 0; i < ID.length; i++){
            System.out.println(ID[i]);
        }
    }
}

Frame - Store an instance of the CodeSolver and make the getCode() method call solve() from above.

public class Frame extends JFrame {

    String code;
    JPanel panel;
    JLabel label;

    CodeSolver solver;

    public Frame(String codeToSolve){
        initGUI();
        this.solver = new CodeSolver(codeToSolve);
    }

    private void initGUI() {
        this.setTitle("CodeSolver");
        this.setSize(600, 300);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setLayout(null);

        panel = new JPanel();
        panel.setBounds(0, 0, 600, 300);
        panel.setBackground(Color.BLACK);

        label = new JLabel(code);
        label.setBounds(0, 0, 600, 300);
        label.setForeground(Color.GREEN);
        label.setFont(new Font("Helvetica New", Font.PLAIN, 60));

        panel.add(label);
        this.add(panel);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public String getCode(){
        return this.solver.solve();
    }
}

Main

Here is where you can pass in the string that you want to solve. Also, run the GUI on a proper Thread.

public class Main {
    public static void main(String[] args) {
        runCodeSolver("cricket_007");
    }

    public static void runCodeSolver(final String code) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frame frame = new Frame(code);
                frame.setVisible(true);
            }
        });
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245