数组更改元素的问题

时间:2015-03-26 02:15:55

标签: java arrays if-statement elements

我正在尝试使用Jbutton将数组中的元素更改为单词“empty”,如果数组中所选位置的空白,则还要通过Jtextfield添加名称。出于某种原因,我无法让它发挥作用。这里的代码不知道我是否遗漏了什么或者我完全错了

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);

不同的方法:

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 个答案:

答案 0 :(得分:0)

向我跳出的第一件事是您使用occupant[selectedApartment] = "Empty";将公寓设置为空,但使用if (occupant[selectedApartment].equals("empty"))来测试公寓是否为空

"Empty" != "empty"

你可以改变

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

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

或使用

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

或更改

occupant[selectedApartment] = "Empty";

occupant[selectedApartment] = "empty";