Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3677472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:13:32+00:00 2026-05-19T03:13:32+00:00

I have a String named updatedDisplay that is set to empty in the constructor.

  • 0

I have a String named updatedDisplay that is set to empty in the constructor.
The buttons[] are JButtons and alarmCode is a String field.

I want the user to press four buttons (and they should be concatenated and stored in the updatedDisplay field).

The checkCode() method is executed to try match updatedDisplay against alarmCode. Trouble is, they never match. I think it may be something to do with a “space” when I originally declare my updatedDisplay as follows:

private String updatedDisplay = ” “;

The updatedDisplay field doesn’t seem to be storing the e.getActionCommand() value.

//add actionListeners to each button (except the "clear" button) to display value on screen
for (int i = 0; i< (buttons.length -1); i++) {
  buttons[i].addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e)
          {
          //store the name of the button in a local variable
          String command = e.getActionCommand();

          System.out.println("You clicked " + command);

          updatedDisplay = updatedDisplay + command;
          //updatedDisplay = command;
          System.out.println (updatedDisplay);

          screen.setText(updatedDisplay);
}
     });}

I have an armButton that, when pressed, should trigger the checkCode() method. The method checks if updatedDisplay and alarmCode are equal:

//add actionListener to the arm button
armButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
         checkCode(); 
        }
    });

checkCode():

public void checkCode() {
//check if user entered the correct code
if (updatedDisplay == alarmCode)
{
  updatedDisplay =  "System Armed!";
  screen.setText(updatedDisplay);
}
else
{
  updatedDisplay  = "Incorrect Code, Try again!";
  screen.setText(updatedDisplay);
}
}

Even when I output the button presses to the terminal window they look right – but as I said, I suspect a “space” is being entered at the start.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:13:33+00:00Added an answer on May 19, 2026 at 3:13 am

    Solution

    Try:

    if( updatedDisplay.equals( alarmCode ) { // ...
    

    Comparison

    To understand this, read:

    http://leepoint.net/notes-java/data/expressions/22compareobjects.html

    Summary

    Since updatedDate and alarmCode are object references, you must ask the objects to compare their values. You can think of them as pointers whose values are locations in memory that contain strings. Rather than comparing the value of the pointers (references), you want to compare the text that starts at that memory location.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.