I have just been learning Java this year, and i am to construct a virtual ATM machine. I stumbled across an "incompatible type" error.
Here is my entire Syntax Code:

 import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

    public class BankMachine extends JFrame implements ActionListener   { 
      JFrame FraWelcome, FraRegis, FraLogin, FraRegised, ErrorRegis, HelpMesg, FraLoginsucc, FraMenu, FraLoginfail;
      JPanel contentPane, contentPaneRegis, contentPaneLogin, contentPaneRegised, contentPaneErrorRegis,contentPaneHelpMesg, contentPaneLoginsucc, contentPaneMenu, contentPaneLoginfail;
      JLabel label, Regislabel, Loginlabel, Regisedlabel, Regisedlabel1, ErrorRegislabel, ErrorRegislabel1, HelpMesglabel, HelpMesglabel1, HelpMesglabel2, HelpMesglabel3, HelpMesglabel4, HelpMesglabel5, HelpMesglabel0, Loginsucclabel, Loginsucclabel1, Loginfaillabel, Loginfaillabel1;
      JTextField textForUser, textForPass, textLogUser;
      JPasswordField PassEcho;
      String User, Pass;

   // Welcome Window
       public BankMachine(){
         FraWelcome = new JFrame ("TD Canada Trust");      
         FraWelcome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         contentPane = new JPanel();
         contentPane.setLayout(new FlowLayout());
         contentPane.setBackground(Color.green);
         contentPane.setBorder(BorderFactory.createEmptyBorder(5,35,5,35));
         FraWelcome.add(Box.createRigidArea(new Dimension(0,100)));

         label = new JLabel("Welcome to TD Canada Trust!");
         contentPane.add(label);

         JButton baccount = new JButton("New Account");
         baccount.addActionListener(this);
         contentPane.add(baccount);

         JButton bsignin = new JButton("Sign In");
         bsignin.addActionListener(this);         
         contentPane.add(bsignin);

         JButton exit = new JButton("Exit");
         exit.addActionListener(this);
         contentPane.add(exit);

         FraWelcome.setContentPane(contentPane);
         FraWelcome.setSize(300,200);
         FraWelcome.setVisible(true);
         FraWelcome.setResizable(false);

      // Registration Window

         FraRegis = new JFrame ("Registration");
         FraRegis.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneRegis = new JPanel();
         contentPaneRegis.setLayout(new FlowLayout());
         contentPaneRegis.setBackground(Color.green);
         contentPaneRegis.add(Box.createRigidArea(new Dimension(0,100)));

         Regislabel = new JLabel("Register Your Username: ");
         contentPaneRegis.add(Regislabel);

         textForUser = new JTextField(6);
         contentPaneRegis.add(textForUser);

         JLabel RegislabelPass = new JLabel("Register Your Password: ");
         contentPaneRegis.add(RegislabelPass);

         textForPass = new JTextField(6);
         contentPaneRegis.add(textForPass);

         JButton register = new JButton("Register");
         register.setActionCommand ("Register");
         register.addActionListener(this);
         contentPaneRegis.add(register);

         JButton back = new JButton("Back");
         back.addActionListener(this);
         contentPaneRegis.add(back);         


         FraRegis.setContentPane(contentPaneRegis);
         FraRegis.setSize(300,200);
         FraRegis.setVisible(false);
         FraRegis.setResizable(false);

      //Log In Frame

         FraLogin = new JFrame ("Log In");
         FraLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         contentPaneLogin = new JPanel();
         contentPaneLogin.setLayout(new FlowLayout());
         contentPaneLogin.setBackground(Color.green);
         contentPaneLogin.add(Box.createRigidArea(new Dimension(0,100)));

         Loginlabel = new JLabel("Enter Your Username: ");
         contentPaneLogin.add(Loginlabel);

         textLogUser = new JTextField(6);
         contentPaneLogin.add(textLogUser);

         JLabel LoginlabelPass = new JLabel("Enter Your Pin: ");
         contentPaneLogin.add(LoginlabelPass);

         PassEcho = new JPasswordField(5);
         contentPaneLogin.add(PassEcho);

         User = textForUser.getText();

         Pass = textForPass.getText();

         JButton Login = new JButton("Log In");
         Login.addActionListener(this);
         contentPaneLogin.add(Login);

         JButton back1 = new JButton("Back");
         back1.addActionListener(this);
         contentPaneLogin.add(back1);   

         FraLogin.setContentPane(contentPaneLogin);
         FraLogin.setSize(300,200);
         FraLogin.setVisible(false);
         FraLogin.setResizable(false);

      // Successful Registration Window

         FraRegised = new JFrame ("Registered!");
         FraRegised.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneRegised = new JPanel();
         contentPaneRegised.setLayout(new FlowLayout());
         contentPaneRegised.setBackground(Color.green);
         contentPaneRegised.add(Box.createRigidArea(new Dimension(0,100)));

         Regisedlabel = new JLabel("Congradulations!");
         contentPaneRegised.add(Regisedlabel);

         Regisedlabel1 = new JLabel("You have now successfully registered your account!");
         contentPaneRegised.add(Regisedlabel1);


         JButton conti = new JButton("Continue");
         conti.addActionListener(this);
         contentPaneRegised.add(conti);

         FraRegised.setContentPane(contentPaneRegised);
         FraRegised.setSize(450,200);
         FraRegised.setVisible(false);
         FraRegised.setResizable(false);

      // Error Registration Window

         ErrorRegis = new JFrame ("Error");
         ErrorRegis.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneErrorRegis = new JPanel();
         contentPaneErrorRegis.setLayout(new FlowLayout());
         contentPaneErrorRegis.setBackground(Color.green);
         contentPaneErrorRegis.add(Box.createRigidArea(new Dimension(0,100)));

         ErrorRegislabel = new JLabel("An Error has occured!");
         contentPaneErrorRegis.add(ErrorRegislabel);

         ErrorRegislabel1 = new JLabel("Please try again or contact one of our agents.");
         contentPaneErrorRegis.add(ErrorRegislabel1);

         JButton Help = new JButton("Help");
         Help.addActionListener(this);
         contentPaneErrorRegis.add(Help);

         JButton cancel = new JButton("Return");
         cancel.addActionListener(this);
         contentPaneErrorRegis.add(cancel);

         ErrorRegis.setContentPane(contentPaneErrorRegis);
         ErrorRegis.setSize(300,200);
         ErrorRegis.setVisible(false);
         ErrorRegis.setResizable(false);

      //Help Message Window

         HelpMesg = new JFrame ("Help");
         HelpMesg.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneHelpMesg = new JPanel();
         contentPaneHelpMesg.setLayout(new FlowLayout());
         contentPaneHelpMesg.setBackground(Color.green);
         contentPaneHelpMesg.add(Box.createRigidArea(new Dimension(0,100)));

         HelpMesglabel = new JLabel("You have encountered an error!");
         contentPaneHelpMesg.add(HelpMesglabel);

         HelpMesglabel1 = new JLabel("Please read the following if you are experiencing technical difficulties: ");
         contentPaneHelpMesg.add(HelpMesglabel1);

         HelpMesglabel2 = new JLabel("1.) Your registration are lacking either the Username or the Password.");
         contentPaneHelpMesg.add(HelpMesglabel2);

         HelpMesglabel3 = new JLabel("2.) Your registration needs to be longer than 4 characters or numbers.");
         contentPaneHelpMesg.add(HelpMesglabel3);

         HelpMesglabel4 = new JLabel("3.) Your registration contains !@#$ or illegal letters.");
         contentPaneHelpMesg.add(HelpMesglabel4);

         HelpMesglabel5 = new JLabel("4.) Your Moron Quotient count is way too high.");
         contentPaneHelpMesg.add(HelpMesglabel5);

         HelpMesglabel0 = new JLabel("5.) You are either Lahiru Pinna-whatever, Ryan'DRAGON'Shek or Antonio'Late'Kim.");
         contentPaneHelpMesg.add(HelpMesglabel0);

         JButton Okay = new JButton("Okay");
         Okay.addActionListener(this);
         contentPaneHelpMesg.add(Okay);

         HelpMesg.setContentPane(contentPaneHelpMesg);
         HelpMesg.setSize(500,400);
         HelpMesg.setVisible(false);
         HelpMesg.setResizable(false);

      // Successful Log In Window

         FraLoginsucc = new JFrame ("Logged In!");
         FraLoginsucc.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneLoginsucc = new JPanel();
         contentPaneLoginsucc.setLayout(new FlowLayout());
         contentPaneLoginsucc.setBackground(Color.green);
         contentPaneLoginsucc.add(Box.createRigidArea(new Dimension(0,100)));

         Loginsucclabel = new JLabel("Congradulations!");
         contentPaneLoginsucc.add(Loginsucclabel);

         Loginsucclabel1 = new JLabel("You have now successfully signed in your account!");
         contentPaneLoginsucc.add(Loginsucclabel1);

         JButton continu = new JButton("Advance");
         continu.addActionListener(this);
         contentPaneLoginsucc.add(continu);

         FraLoginsucc.setContentPane(contentPaneLoginsucc);
         FraLoginsucc.setSize(450,200);
         FraLoginsucc.setVisible(false);
         FraLoginsucc.setResizable(false);

      // Fail Log In Window

         FraLoginfail = new JFrame ("Failed!");
         FraLoginfail.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneLoginfail = new JPanel();
         contentPaneLoginfail.setLayout(new FlowLayout());
         contentPaneLoginfail.setBackground(Color.green);
         contentPaneLoginfail.add(Box.createRigidArea(new Dimension(0,100)));

         Loginfaillabel = new JLabel("Uh Oh!");
         contentPaneLoginfail.add(Loginfaillabel);

         Loginfaillabel1 = new JLabel("The Username and/or the password you entered does not match.");
         contentPaneLoginfail.add(Loginfaillabel1);

         JButton retry = new JButton("Try Again");
         retry.addActionListener(this);
         contentPaneLoginfail.add(retry);

         FraLoginfail.setContentPane(contentPaneLoginfail);
         FraLoginfail.setSize(450,200);
         FraLoginfail.setVisible(false);
         FraLoginfail.setResizable(false);










      //Main Menu Window

         FraMenu = new JFrame ("Logged In!");
         FraMenu.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         contentPaneMenu = new JPanel();
         contentPaneMenu.setLayout(new FlowLayout());
         contentPaneMenu.setBackground(Color.green);
         contentPaneMenu.add(Box.createRigidArea(new Dimension(0,100)));

         JButton Withdraw = new JButton("Withdraw");
         Withdraw.addActionListener(this);
         contentPaneMenu.add(Withdraw);

         JButton QuickWithdraw = new JButton("Quick Withdrawal");
         QuickWithdraw.addActionListener(this);
         contentPaneMenu.add(QuickWithdraw); 

         JButton Deposit = new JButton("Deposit");
         Deposit.addActionListener(this);
         contentPaneMenu.add(Deposit);

         JButton QuickDeposit = new JButton("Quick Deposit");
         QuickDeposit.addActionListener(this);
         contentPaneMenu.add(QuickDeposit);

         JButton AccountManagement = new JButton("Account Management");
         AccountManagement.addActionListener(this);
         contentPaneMenu.add(AccountManagement);

         JButton CheckBalance = new JButton("Check Balance");
         CheckBalance.addActionListener(this);
         contentPaneMenu.add(CheckBalance);

         JButton Transfer = new JButton("Transfer");
         Transfer.addActionListener(this);
         contentPaneMenu.add(Transfer);

         JButton End = new JButton("End Transition");
         End.addActionListener(this);
         contentPaneMenu.add(End);










         FraMenu.setContentPane(contentPaneMenu);
         FraMenu.setSize(450,200);
         FraMenu.setVisible(false);
         FraMenu.setResizable(false);














      }

       public void actionPerformed(ActionEvent e)
      {
         if (e.getActionCommand().equals("New Account"))
         {
            FraRegis.setVisible(true);
            FraWelcome.setVisible(false);  

         }
         else if(e.getActionCommand().equals("Sign In"))
         {
            FraLogin.setVisible(true);
            FraWelcome.setVisible(false);       
         }
         else if(e.getActionCommand().equals("Exit"))
         {
            System.exit(0);
         }
         if (e.getActionCommand().equals("Register"))
         {
            if (textForUser.getText().equals(""))
            {
               FraRegis.setVisible(false);
               ErrorRegis.setVisible(true);
            }
            else if (textForPass.getText().equals(""))
            {
               FraRegis.setVisible(false);
               ErrorRegis.setVisible(true);
            }
            else
            {
               FraRegis.setVisible(false);
               FraRegised.setVisible(true);
            }
         }
         if (e.getActionCommand().equals("Back"))
         {
            FraLogin.setVisible(false);
            FraRegis.setVisible(false);
            FraWelcome.setVisible(true);    
         }

         if (e.getActionCommand().equals("Continue"))
         {
            FraRegised.setVisible(false);
            FraWelcome.setVisible(true);
         }
         if (e.getActionCommand().equals("Return"))
         {
            ErrorRegis.setVisible(false);
            FraRegis.setVisible(true);
         }

         if (e.getActionCommand().equals("Help"))
         {
            ErrorRegis.setVisible(false);
            HelpMesg.setVisible(true);
         }

         if (e.getActionCommand().equals("Okay"))
         {
            HelpMesg.setVisible(false);
            FraRegis.setVisible(true);
         }

         if (e.getActionCommand().equals("Log In"))
         {
            if (User = textLogUser)
            { 
               FraLogin.setVisible(false);
               FraLoginsucc.setVisible(true);
            }
            if (Pass = PassEcho)
            {
               FraLogin.setVisible(false);
               FraLoginsucc.setVisible(true);
            }
            else 
            {
               FraLogin.setVisible(false);
               FraLoginfail.setVisible(true);
            }
         }

         if (e.getActionCommand().equals("Advance"))
         {
            FraLoginsucc.setVisible(false);
            FraMenu.setVisible(true);
         }



      }










    /*
     try 
    {
     //convert gettext to int
    // Error Message
     catch (NumberFormatException e)
     {
     Rest of program
    }*/

       private static void runGUI() {
         JFrame.setDefaultLookAndFeelDecorated(true);

         BankMachine greeting = new BankMachine();

      }

       public static void main (String[]args) {

         javax.swing.SwingUtilities.invokeLater(
                new Runnable() {
                   public void run() {
                     runGUI();
                  }
               });
      }
   }

Can you spot my problem? :S
Please and thank you in advance!

Recommended Answers

All 5 Replies

Please use code tags while posting code. Also mention the section that you are getting this particular error in.

Incompatible type errors are generated when the compiler expects an identifier to be of a certain type and finds one having another. So just try and find out if you can where such a situation might be happening in your code.

It might be because of the equals you are using in the if statements.Try to convert it into toString() first

eg: if(e.getActionCommand().toString().equals("Okay"))

you are comparing an object with a string,you should compare 2 strings instead.


Hope this solves your problem

> AbhikGhosh

Firstly, the getActionCommand() methods of the ActionEvent class returns a string which he is comparing to another string using equals, thats correct behaviour. And secondly the toString method will not necessarily return the action command associated with it, but the string representation of the object. It depends on how the toString() method is implemented in the lowest class in the hierarchy, since the toString method belongs to the Object Class.

The problem is in these two lines:

if (User = textLogUser)

and

if (Pass = PassEcho)

User and Pass are strings whereas textLogUser is a JTextField and PAssEcho is a JPasswordField. Also you are not even comparing here, what you assume to be doing, but rather just assigning these values to the Strings directly. If you need to compare the values from the text/password fields to the ones in the Strings what you should be doing is this:

If(User.equals(textLogUser.getText()))

and

If (Pass.equals(new String(PassEcho.getPassword())))

Note: We are using the getPassword method here which returns a char [] and hence we use the String class' appropriate constructor to convert it into a String object before being passed to the equals method.

yes he is correct...

getUser line is problem...

i tried but before veruckt suggest right...

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.