Program not comparing JFrame textfield value with number generated in other class

Viewed 32

I made a little random math generator, which gives out 2 random numbers and a random operator but when I call my check result method in my ok button, I always get the "Errado" ("Wrong") string which means it's not properly comparing the value of the textfield with the result of whatever math problem was presented. Leaving relevent code from both classes down here with comments on where I'm having issues and 2 for ease of understanding. Help's appreciated!

public class geraAritmetica {
    ArrayList<Integer> nums = new ArrayList();
    int a;
    char ops = '?';
    int max = 10;
    int min = 1;
    int range = max - min + 1;
    int res;

    Random r = new Random();
    
    public int gerarNums(){
        for(int i = 0; i<10; i++){
            a = (int) (Math.random()*range) + min;
            nums.add(a);
        }
        return a;
    }
    
    public char gerarOps(){
        switch(r.nextInt(4)){
            case 1 : ops = '+';
                     res = nums.get(0) + nums.get(1);
            break;
            case 2: ops = '-';
                    res = nums.get(0) - nums.get(1);
                    break;
            case 3: ops = '*';
                    res = nums.get(0) * nums.get(1);
                    break;
            case 4: ops = '/';
                    res = nums.get(0) / nums.get(1);
                    break;    
            
            default: ops = '+';
                     res = nums.get(0) + nums.get(1);
            break;
        }
        return ops;
    }
        
        public boolean checkRes(){   //everytime I call this method always get the same string "Errado" ("Wrong") in the label.          
            if(Integer.parseInt(FrameEnsinoAritmetica.jTextField1.getText()) == res){
                FrameEnsinoAritmetica.jLabel2.setText("Correto!");
                return true;
            }
            else FrameEnsinoAritmetica.jLabel2.setText("Errado!");
            return false;
        }
        
    }

public class FrameEnsinoAritmetica extends javax.swing.JFrame {
   geraAritmetica a = new geraAritmetica(); //creating object of class at the start of my JFrame class
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  //generating numbers and operator (it prints fine in another label)                                       

        int b = a.gerarNums();
        int c = a.gerarNums();
        char s = a.gerarOps();
        
        jLabel1.setText(b + " " +s+ " " + c);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                            
        a.checkRes(); //everytime I call this method always get the same string "Errado" ("Wrong") in the label.          
    }  
2 Answers

You seem to be referencing to the FrameEnsinoAritmetica class directly not the actual instance of your FrameEnsinoAritmetica jFrame that you see on screen. For example, using this in another class will not reference to the frame that you see on screen:

String text = FrameEnsinoAritmetica.jTextField1.getText();

But using this would work:

String text = yourInstanceOfEnsinoAritmetica.jTextField1.getText();

To fix this we could pass the current instance of the FrameEnsinoAritmetica that is shown on screen into the checkRes method, which we do by modifying checkRes like so, and inside the method we replace FrameEnsinoAritmetica with frame to refer to the current instance:

    public boolean checkRes(FrameEnsinoAritmetica frame){   //everytime I call this method always get the same string "Errado" ("Wrong") in the label.          
        if(Integer.parseInt(frame.jTextField1.getText()) == res){
            frame.jLabel2.setText("Correto!");
            return true;
        }
        else frame.jLabel2.setText("Errado!");
        return false;
    }

Then we just update the jButton1ActionPerformed method to pass this which is the current instance of FrameEnsinoAritmetica into the updated checkRes method:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                            
    a.checkRes(this); //everytime I call this method always get the same string "Errado" ("Wrong") in the label.          
} 

Fix part 2 (for res not being calculated properly)


public class FrameEnsinoAritmetica extends javax.swing.JFrame {
   geraAritmetica a = new geraAritmetica();
   public static int b;
   public static int c;
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        b = a.gerarNums();
        c = a.gerarNums();
        char s = a.gerarOps(this);
        
        jLabel1.setText(b + " " +s+ " " + c + " ");
    }  


public class geraAritmetica {
    ArrayList<Integer> nums = new ArrayList();
    int a;
    char ops = '?';
    int max = 10;
    int min = 1;
    int range = max - min + 1;
    public static int res;

    Random r = new Random();

    public int gerarNums(){
        for(int i = 0; i<10; i++){
            a = (int) (Math.random()*range) + min;
            nums.add(a);
        }
        return a;
    }

    public char gerarOps(FrameEnsinoAritmetica frame){
        switch(r.nextInt(4)){
            case 1 : ops = '+';
                     res = frame.b + frame.c; //checking both variables directly from JFrame class instead of getting through index from ArrayList.
                    break;
            case 2: ops = '-';
                    res = res = frame.b - frame.c;
                    break;
            case 3: ops = '*';
                    res = frame.b * frame.c;
                    break;
            case 4: ops = '/';
                    res = frame.b / frame.c;
                    break;    
            
            default: ops = '+';
                     res = frame.b + frame.c;
            break;
        }
        return ops;
    }
        
        public boolean checkRes(FrameEnsinoAritmetica frame){
            if(Integer.parseInt(frame.jTextField1.getText()) == res){
                frame.jLabel2.setText("Correto!");
                return true;
            }
            else frame.jLabel2.setText("Errado!");
            return false;
        }
        
    }
Related