Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

in if condition arraylist .startsWith not correct working

could someone tell me why my if statement with .startsWith is true even though the String (booking) does not start with A? And how to fix it, if possible. thanks in advance!

  List<Film> filmList = new ArrayList<>();
        filmList.add(new Film("Batman", "2D", "20:15", "Room 1", "Available"));
        filmList.add(new Film("Batman Returns", "3D", "22:30", "Room 1", "Available"));
        filmList.add(new Film("Batman Returns Again", "3D", "20:15", "Room 2", "Available"));
        filmList.add(new Film("Batman Never Dies", "2D", "23:30", "Room 2", "Booked Out"));

        //printing movie list
        for (Film film : filmList) {
            System.out.println(film.name + " | " + film.quality + " | " + film.time + " | " + film.room + " | " + film.booking);
        }

        //picking movie
        System.out.println();
        System.out.println("Choose a movie that you want to see!");
        String filmPick = scan.next().toLowerCase(Locale.ROOT);

        Film pickedfilm = null;
        for (Film film : filmList) {
            if (film.name.equalsIgnoreCase(filmPick) && film.booking.toLowerCase().startsWith("a")) {
                pickedfilm = film;
                System.out.println("Your booking was successfully");
            } else {
                System.out.println("No movie found!");
            }
            break;
        }

//class

class Film {
    public String name;
    public String quality;
    public String time;
    public String room;
    public String booking;

    public Film(String name, String quali, String time, String room, String booking) {
        this.name = name;
        this.quality = quali;
        this.time = time;
        this.room = room;
        this.booking = booking;
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should read nextLine. next() method finds and returns the next complete token from this scanner. A complete token is preceded and followed by the input that matches the delimiter pattern.

Whenever you are adding "Batman Never Dies", it read "Batman", and your for-loop will conclude this is present.

replace this line

String filmPick = scan.next().toLowerCase(Locale.ROOT);

with this:-

 String filmPick = scan.nextLine().toLowerCase(Locale.ROOT);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading