简体   繁体   中英

Find all matching elements in Array - Java

At the moment my code is only giving me the first matching result. The user inputs their desired room price and at the moment it will only display the first match. In the case the user inputs '60' to the console it should display 3 results. I imagine i'll need another forloop and if statement after it prints to the console but not sure how to execute

public static void secondMain() {
    BufferedReader reader;
    var lines = new ArrayList<String>();
    var rooms = new ArrayList<Room>();
    Room selectedRoom = null;

    try {
        reader = new BufferedReader(new FileReader("rooms.txt"));
        String line = reader.readLine();
        lines.add(line);
        while (line != null) {
            line = reader.readLine();
            lines.add(line);

        }

        reader.close();

        for (int i = 0; i < lines.size() - 1; i++) {
            String[] words = lines.get(i).split(" ");
            var room = new Room();
            room.roomNum = Integer.parseInt(words[0]);
            room.roomType = (words[1]);
            room.roomPrice = Double.parseDouble(words[2]);
            room.hasBalcony = Boolean.parseBoolean(words[3]);
            room.hasLounge = Boolean.parseBoolean(words[4]);
            room.eMail = (words[5]);
            rooms.add(room);
        }

        var searchRoomPrice = input.nextDouble();

        for (int i = 0; i < rooms.size(); i++) {
            if (rooms.get(i).roomPrice == searchRoomPrice) {
                selectedRoom = rooms.get(i);
                break;
            }
        }

        System.out.println("Room Number: " + selectedRoom.roomNum);
        System.out.println("Room Type: " + selectedRoom.roomType);
        System.out.println("Room Price: " + selectedRoom.roomPrice);
        System.out.println("Balcony: " + selectedRoom.hasBalcony);
        System.out.println("Lounge: " + selectedRoom.hasLounge);
        System.out.println("Email: " + selectedRoom.eMail);
        System.out.println("-------------------");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Any other information needed feel free to ask

If you only want to print the information move the print commands inside the loop and remove the break ie

for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         selectedRoom = rooms.get(i);
         System.out.println("Room Number: " + selectedRoom.roomNum);
         System.out.println("Room Type: " + selectedRoom.roomType);
         System.out.println("Room Price: " + selectedRoom.roomPrice);
         System.out.println("Balcony: " + selectedRoom.hasBalcony);
         System.out.println("Lounge: " + selectedRoom.hasLounge);
         System.out.println("Email: " + selectedRoom.eMail);
         System.out.println("-------------------");
    }   
}
            

You could also save all the objects in a list with the first loop and then in a second loop iterate over the list and print the information ie

List<Room> roomList = new ArrayList<Room>();
for(int i = 0; i < rooms.size(); i++){
     if(rooms.get(i).roomPrice == searchRoomPrice){
         roomList.add(rooms.get(i));             
    }   
}
for(Room room : roomList){
    System.out.println("Room Number: " + room.roomNum);
    System.out.println("Room Type: " + room.roomType);
    System.out.println("Room Price: " + room.roomPrice);
    System.out.println("Balcony: " + room.hasBalcony);
    System.out.println("Lounge: " + room.hasLounge);
    System.out.println("Email: " + room.eMail);
    System.out.println("-------------------");
}       

I notice 2 things:

  1. You are using a break; here:
                if(rooms.get(i).roomPrice == searchRoomPrice){
                    selectedRoom = rooms.get(i);
                    break;
                }   

So you are stopping the loop after the first match.

  1. Here:
for (int i = 0; i < lines.size() - 1; i++)

Is there a reason to use that minus 1?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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