0

I am trying to change elements in an array to the word "empty" with a Jbutton and also add names through a Jtextfield if the in the selected position in the array says empty. For some reason I cant get it to work. here is the code don't know if I am missing something or I am just completely wrong

move = new JButton("Add Tennant");
window.add(move);
moveIn.addActionListener(this);

Tennant = new JTextField(FIELD_WIDTH);
nTennant.setText("Enter new name") ;
window.add(Tennant);
Tennant.addActionListener(this);

evict = new JButton("Evict");
window.add(evict);
moveIn.addActionListener(this);

different method:

if(e.getSource() == move)
{
    if (occupant[selectedApartment].equals("empty"))
    {
        occupant[selectedApartment] = Tennant.getText();
    }
}

if(e.getSource() == evict)
{
    if(!occupant[selectedApartment].equals("Empty"))
    {
        occupant[selectedApartment] = "Empty";
    }
}
1
  • Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses Commented Mar 26, 2015 at 2:17

1 Answer 1

1

The first thing that jumps out at me is you use occupant[selectedApartment] = "Empty"; to set an apartment empty, but use if (occupant[selectedApartment].equals("empty")) to test if an apartment is empty

"Empty" != "empty"

You could change

if (occupant[selectedApartment].equals("empty"))

to

if (occupant[selectedApartment].equals("Empty"))

or use

if (occupant[selectedApartment].equalsIgnoreCase("empty"))

or change

occupant[selectedApartment] = "Empty";

to

occupant[selectedApartment] = "empty";
Sign up to request clarification or add additional context in comments.

1 Comment

I used if (occupant[selectedApartment].equalsIgnoreCase("empty")) and that worked but still working on the evict function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.