What are the reasons for “Cannot find a character” and how to fix it?

I tried to figure this out, I ran it in different programs, so it is definitely in the code. Probably also something easy. Error says

Password2.java:90: error: cannot find character if (pw.equals (password)) ^ character: variable password location: class Password2.EnterButtonHandler 1 error

Here is the code:

// Password1.java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Password2 extends JFrame // inherits from the JFrame class { // static final variables to hold frame dimensions (in pixels) private static final int WIDTH = 400; private static final int HEIGHT = 120; //declare labels, fields, buttons, etc. private JLabel enterLabel, validLabel, resultLabel; private JTextField pwTextField; private JButton enterB, clearB; private EnterButtonHandler ebHandler; private ClearButtonHandler cbHandler; public Password2() // constructor defines frame { setTitle( "Password Checker" ); // set the title of the frame setSize( WIDTH, HEIGHT ); // set the frame size // prepare the container Container pane = getContentPane(); GridLayout aGrid = new GridLayout( 3, 2, 5, 5 ); // create a 3 row 2 column layout pane.setLayout( aGrid ); // set the layout for the frame String password = "hello"; //instantiate JLabels enterLabel = new JLabel("Enter Password: "); validLabel = new JLabel("Validation: "); resultLabel = new JLabel(""); //instantiate text fields pwTextField = new JPasswordField( 30 ); //instantiate buttons enterB = new JButton("Enter"); clearB = new JButton("Clear"); //initialize button handler ebHandler = new EnterButtonHandler(); enterB.addActionListener(ebHandler); //initialize button handler cbHandler = new ClearButtonHandler(); clearB.addActionListener(cbHandler); pane.add(enterLabel); pane.add(pwTextField); pane.add(validLabel); pane.add(resultLabel); pane.add(enterB); pane.add(clearB); //calls center frame method centerFrame( WIDTH, HEIGHT ); }// end constructor //methood to center GUI on screen public void centerFrame( int frameWidth, int frameHeight) { //create toolkit object Toolkit aToolkit = Toolkit.getDefaultToolkit(); //create a dimension object with user screen information Dimension screen = aToolkit.getScreenSize(); //assign x, y position of upper left corner of frame int xUpperLeft = ( screen.width - frameWidth ) / 2; int yUpperLeft = ( screen.height - frameHeight ) / 2; //method to position frame on user screen setBounds( xUpperLeft, yUpperLeft, frameWidth, frameHeight ); } private class EnterButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String pw = pwTextField.getText(); if(pw.equals(password)) { resultLabel.setText("Password Accepted"); pwTextField.setText(""); } else { resultLabel.setText("Password Rejected"); pwTextField.setText(""); } } } private class ClearButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { resultLabel.setText(""); pwTextField.setText(""); } } public static void main(String [] args) { JFrame aPassword2 = new Password2(); // create the JFrame object aPassword2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aPassword2.setVisible(true); } } // end of class 
+6
source share
5 answers

Read the error message, love the error message.

This requires some practice, but after a while it is easy to see it more clearly: just read the bold text below as a suggestion :)

error: cannot find character [...]

: variable password

location: [in] class Password2. EnterButtonHandler

There is nothing password ( EnterButtonHandler ) in this area / context.

Happy coding.


Hint: is there a local variable with the same name in a different scope / context ... maybe it should not be a local variable? See Java Tutorial: Variables for more :)

+11
source

password is local to the Password2 constructor.

It must either be passed in or an instance variable.

0
source

Your class has no definition for password . Therefore, an error occurred while passing it to the equals method.

0
source

It is not possible to find the password variable, which, as you encoded it, exists only in the Password2 constructor. You need to either make the password a member variable of the class, or pass it to the constructor of your Handler classes so that they can refer to it.

0
source
 password 

is a local variable declared in the Password2 constructor. This is not part of your EnterButtonHandler.actionPerformed method . Create an instance variable for it.

0
source

Source: https://habr.com/ru/post/901244/


All Articles