gpt4 book ai didi

java - 检查 ArrayList 中是否存在 JTextfield 值

转载 作者:行者123 更新时间:2023-12-02 02:06:24 25 4
gpt4 key购买 nike

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

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 类(class):包实体;

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

}

否则,当然,我想成功写入文件。我哪里错了?

最佳答案

“最简单”的路线需要您循环遍历 rooms List 中的每个 Room 并比较 roomNumber code> 与 Room 的房间号

类似...

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

注意:我没有您的 Room 类,因此我不得不“猜测”它的功能

“更简洁”的方式可能是利用流的 API

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

关于java - 检查 ArrayList 中是否存在 JTextfield 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50732703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com