检查ArrayList

时间:2018-06-07 03:54:24

标签: java swing arraylist jtextfield

我正在尝试检查数组列表中是否存在输入的房间号:

private void initAction(RoomsWindow rw, RoomAddUpdateWindow raw) {
            btnConfirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ArrayList<Room> rooms = RoomsWindow.loadRoom();
                    for (Room r : rooms) {
                        r.getNumber();
                    }
                    if (rooms.equals(txtNumber.getText())) {
                        JOptionPane.showMessageDialog(null, "Room number already exists!", "Add",
                                JOptionPane.PLAIN_MESSAGE);
                    } else {
                        File fileRooms = new File("src/txt/rooms");
                        try {
                            FileWriter fw = new FileWriter(fileRooms, true);
                            BufferedWriter bw = new BufferedWriter(fw);
                            PrintWriter pw = new PrintWriter(bw, true);
                            String tv = (String) cbTV.getSelectedItem();
                            if (cbTV.getSelectedItem().equals("Yes")) {
                                tv = "true";
                            } else {
                                tv = "false";
                            }
                            String miniBar = (String) cbMiniBar.getSelectedItem();
                            if (cbMiniBar.getSelectedItem().equals("Yes")) {
                                miniBar = "true";
                            } else {
                                miniBar = "false";
                            }
                            pw.println(txtNumber.getText() + "|" + txtType.getText() + "|" + txtName.getText() + "|"
                                    + txtBeds.getText() + "|" + tv + "|" + miniBar + "|" + "false" + "|" + "false");
                            pw.close();
                            JOptionPane.showMessageDialog(null, "Succesfully", "Done", JOptionPane.PLAIN_MESSAGE);
                            raw.setVisible(false);
                            rw.setVisible(false);
                            RoomsWindow newRoomWindow = new RoomsWindow();
                            newRoomWindow.setVisible(true);

                        } catch (IOException exc) {
                            exc.printStackTrace();
                        }
                    }
                }
            });
        }

如果它存在,那么我不想写入文件,只是为了在

中打印一条消息
dialog: OptionPane.showMessageDialog(null, "Room number already exists!", "Add",
                                    JOptionPane.PLAIN_MESSAGE);

修改 这是我的Room课程: 包实体;

public class Room {

    int number;
    String type;
    String name;
    int beds;
    Boolean tv;
    Boolean miniBar;
    Boolean ocupied;
    Boolean deleted;
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getBeds() {
        return beds;
    }
    public void setBeds(int beds) {
        this.beds = beds;
    }
    public Boolean getTv() {
        return tv;
    }
    public void setTv(Boolean tv) {
        this.tv = tv;
    }
    public Boolean getMiniBar() {
        return miniBar;
    }
    public void setMiniBar(Boolean miniBar) {
        this.miniBar = miniBar;
    }
    public Boolean getOcupied() {
        return ocupied;
    }
    public void setOcupied(Boolean ocupied) {
        this.ocupied = ocupied;
    }
    public Boolean getDeleted() {
        return deleted;
    }
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }
    public Room(int number, String type, String name, int beds, Boolean tv, Boolean miniBar, Boolean ocupied,
            Boolean deleted) {
        super();
        this.number = number;
        this.type = type;
        this.name = name;
        this.beds = beds;
        this.tv = tv;
        this.miniBar = miniBar;
        this.ocupied = ocupied;
        this.deleted = deleted;
    }

}

另外,当然,我想成功地写文件。我哪里错了?

2 个答案:

答案 0 :(得分:2)

最简单的&#34;路线要求您循环Room rooms中的每个List,并将roomNumberRoom的房间号

像...一样的东西。

List<Room> rooms = RoomsWindow.loadRoom();
// This may throw an exception, so you'll want to deal with that
String roomNumber = Integer.parseInt(txtNumber.getText());
boolean found = false;
for (Room room : rooms) {
    if (room.getNumber() == roomNumber) {
        found = true;
        break;
    }
}

nb:我没有你的Room课,所以我不得不猜测&#34;它的功能

A&#34;更时尚&#34;方法可能是使用流的API

Room match = rooms.stream().filter(room -> room.getNumber() == roomNumber).orElse(null);
if (match != null) {
    // Found room
} else {
    // Did not find foom
}

答案 1 :(得分:0)

  

希望这会有帮助

通过这种方式,您可以检查列表中的现有值。

if(rooms.contains(txtNumber.getText())){
    ///Do Not add
    }else{
    //add in List
    }
  

所有最佳。