Search code examples
javabuttonif-statementjtextfieldvoid

Java is trying to do if statement that is not true


I'm trying to do a program that if two TextFields are equal to a value something will happen and I have two if statements. When the first if statement happens the second if statement happens but the value I set in the code does not match. To explain better, here is the code:

private void LevelUp(){
        float level = (float) Double.parseDouble(lvl.getText());
        float expmn = (float) Double.parseDouble(ExpMin.getText());
                    if(level == 1  && expmn == 500){
                    lvl.setText("2");
                    ExpMin.setText("501");
                    ExpMax.setText("1000");
                    JOptionPane.showMessageDialog(null,"" + "levevlup!" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
                    if(level == 2 && expmn == 1000); 
                        lvl.setText("3");
                        ExpMin.setText("1001");
                        ExpMax.setText("2500");

        }
        else{
            JOptionPane.showMessageDialog(null,"" + "leveladssadaup" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
        }
    }
}

Solution

  • Not only you should erase the ; after the if-statement where you look for level2, but if I look into your code correctly, you should close your if-statement where you look for level1 before asking for level2 and use else if for the level2.

    Also go for curly braces after the level2 if:

        if(level == 1  && expmn == 500){
                    lvl.setText("2");
                    ExpMin.setText("501");
                    ExpMax.setText("1000");
                    JOptionPane.showMessageDialog(null,"" + "levevlup!" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
        }// <-- close level1 here because you want to check for a new level afterwards
        else if(level == 2 && expmn == 1000){ //<-- here was your ;, which was wrong
                        lvl.setText("3");
                        ExpMin.setText("1001");
                        ExpMax.setText("2500");
    
        }
        else{
            JOptionPane.showMessageDialog(null,"" + "leveladssadaup" ,"Para Yok",JOptionPane.PLAIN_MESSAGE);
        }