Search code examples
javaswingarraylistjtextfield

Check if JTextfield value exist in ArrayList


I'm trying to check does inputed room number exist in array list:

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

If it exist then i don't want to write in file, just to print a message in

dialog: OptionPane.showMessageDialog(null, "Room number already exists!", "Add",
                                    JOptionPane.PLAIN_MESSAGE);

EDIT Here is my Room class: package entities;

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

}

Else, ofcourse, I want to write in file succesfuly. Where am I wrong?


Solution

  • The "simplest" route would require you to loop over each Room in the rooms List and compare the roomNumber with the Room's room number

    Something like...

    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: I don't have your Room class so I had to "guess" at it's functionality

    A "sleeker" way might be to make use of the stream's API

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