-1

In my last else-if-statement (at the end of the program) I print out the two variables (CodeCheck and cPass) I want to check in the following if-else statement. The values are correct, including their length. However, it does not go in to the if statement (cPass == "Skyfall"), meaning it sees it as false. If I assign the String (i.e. cPass = "Skyfall") right before the if-else statement is works (it sees it as true).

I do not understand why, the following does not seem to work in the if-else statement:

cPass = String.format("%s",e.getActionCommand());

and similarly:

CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);

However, it works to print the values of these variables (and their lengths).

Here is all of the code for the program:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class SecurityWindow extends JFrame{


     JButton n1 = new JButton("1");
     JButton n2 = new JButton("2");
     JButton n3 = new JButton("3");
     JButton n4 = new JButton("4");
     JButton n5 = new JButton("5");
     JButton n6 = new JButton("6");
     JButton n7 = new JButton("7");
     JButton n8 = new JButton("8");
     JButton n9 = new JButton("9");

     JTextField username = new JTextField("username");
     JPasswordField password = new JPasswordField("password");

     JButton play;


    public SecurityWindow(){
        super("Security Passage");
        setLayout(new FlowLayout());

        add(username);
        add(password);

        add(n1);
        add(n2);
        add(n3);
        add(n4);
        add(n5);
        add(n6);
        add(n7);
        add(n8);
        add(n9);

        Icon dec = new ImageIcon(getClass().getResource("faceb.png"));
        play = new JButton("Login", dec);
        add(play);

        HandlerClass handler = new HandlerClass();
        username.addActionListener(handler);
        password.addActionListener(handler);
        n1.addActionListener(handler);
        n2.addActionListener(handler);
        n3.addActionListener(handler);
        n4.addActionListener(handler);
        n5.addActionListener(handler);
        n6.addActionListener(handler);
        n7.addActionListener(handler);
        n8.addActionListener(handler);
        n9.addActionListener(handler);
        play.addActionListener(handler);

    }

    int cCode[] = {10,10,10};
    String cUser = "";
    String cPass = "";
    String CodeCheck = "";

    private class HandlerClass implements ActionListener{
        public void actionPerformed(ActionEvent e){

              if(e.getSource() == username){
                 cUser = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == password){
                 cPass = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == n1){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n2){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                     System.out.println(cCode[0]);      
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n3){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n4){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n5){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n6){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n7){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n8){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n9){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == play){

                 CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);

                 System.out.println(CodeCheck);
                 System.out.println(cPass);

                 if(cPass == "Skyfall"){
                     if(CodeCheck == "558"){
                         JOptionPane.showMessageDialog(null, cUser + ", you have cracked the security system");
                     }else{
                     System.out.println("cPass wrong");
                     }
                 }else{
                     JOptionPane.showMessageDialog(null, "No Access Granted " + cUser);
                     /*JOptionPane.showMessageDialog(null, CodeCheck);
                     JOptionPane.showMessageDialog(null, cPass);
                     JOptionPane.showMessageDialog(null, cUser);*/

                     System.out.println(CodeCheck);
                     System.out.println(cPass);
                     System.out.println(cUser);
                     System.out.println(cPass.length());
                     System.out.println(CodeCheck.length());
                 }
             }
        }
    }
}

Thank you in advance.

Iceman
  • 13
  • 4

1 Answers1

0

Do String comparison with equals() method. With == operator, you compare both String's memory addresses. With equals() method, you compare both String's values. So instead of

if(cPass == "Skyfall")

use

if(cPass.equals("Skyfall"))