数组更改元素的问题

问题描述 投票:0回答:1

我试图用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";
    }
}
java arrays if-statement elements
1个回答
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";
© www.soinside.com 2019 - 2024. All rights reserved.