Skip to content
Advertisement

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

Advertisement

Answer

If you only want to print the information move the print commands inside the loop and remove the break i.e.

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 i.e.

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("-------------------");
}       
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement