简体   繁体   English

数组更改元素的问题

[英]Problems with array changing elements

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. 我正在尝试使用Jbutton将数组中的元素更改为单词“ empty”,并且如果数组中所选位置的内容为空,则还会通过Jtextfield添加名称。 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";
    }
}

The first thing that jumps out at me is you use occupant[selectedApartment] = "Empty"; 跳到我身上的第一件事是您使用occupant[selectedApartment] = "Empty"; to set an apartment empty, but use if (occupant[selectedApartment].equals("empty")) to test if an apartment is empty 将公寓设置为空,但使用if (occupant[selectedApartment].equals("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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM