简体   繁体   English

即使所有值都保存在数组中,为什么我的 find 方法在这里不起作用?

[英]Why doesn't my find method work here even with all the values saved on the array?

I am fairly new to programming and I cant seem to find the problem in the area where I make the program search for the specific key term tho it's saved in the array.我对编程相当陌生,我似乎无法在我让程序搜索特定关键字的区域中找到问题,但它已保存在数组中。

here is the code where I add the details of the customer:这是我添加客户详细信息的代码:


private void addCustomerToSeat(String[] seatBooking, String[] customerName, String[] seatBookingAndCustomerName) {
        Button[] seat = new Button[(SEATING_CAPACITY + 1)];

        Button selectSeat = new Button(" Click to Book Seats ");
        selectSeat.setLayoutX(320);
        selectSeat.setLayoutY(512);
        selectSeat.setOnAction(event -> {
            window.setScene(scene2);
        });

        Label header = new Label("TRAIN BOOKING SYSTEM");
        header.setLayoutX(250);
        header.setLayoutY(30);
        header.setStyle("-fx-font-size: 25px;");

        Label clientName = new Label("Enter Customer Name: ");
        clientName.setLayoutX(225);
        clientName.setLayoutY(150);
        clientName.setStyle("-fx-font-size: 16px;");
        TextField cusName = new TextField();
        cusName.setLayoutX(397);
        cusName.setLayoutY(150);



        Label destination = new Label("Choose destination: ");
        destination.setLayoutX(225);
        destination.setLayoutY(200);
        destination.setStyle("-fx-font-size:16px;");

        String [] destinations = {"Colombo to Badulla", "Badulla to Colombo"};
        ComboBox Destination = new ComboBox(FXCollections.observableArrayList(destinations));
        Destination.setLayoutX(397);
        Destination.setLayoutY(200);


        Label date = new Label("Select date:");
        date.setLayoutY(275);
        date.setLayoutX(225);
        date.setStyle("-fx-font-size:16px;");

        DatePicker datePicker = new DatePicker();
        LocalDate now = LocalDate.now();
        datePicker.setValue(now);
        datePicker.setLayoutX(397);
        datePicker.setLayoutY(275);



        AnchorPane layout1 = new AnchorPane();
        layout1.setStyle("-fx-background-color:#5a89a3; ");
        layout1.getChildren().addAll(Destination,destination,selectSeat,clientName,cusName,header,date,datePicker);


        scene1 = new Scene(layout1,800,600);
        window.setTitle("Train Booking System");
        window.setScene(scene1);
        window.show();



        Label header1 = new Label("TRAIN BOOKING SYSTEM");
        header1.setLayoutX(250);
        header1.setLayoutY(30);
        header1.setStyle("-fx-font-size: 25px;");

        Button submit = new Button("Submit");
        submit.setLayoutX(640);
        submit.setLayoutY(480);
        Button exit = new Button("Exit");
        exit.setLayoutX(710);
        exit.setLayoutY(480);
        exit.setOnAction(event -> {
            window.close();
            displayMenu(seatBooking,customerName,seatBookingAndCustomerName);
        });

        Label greenSeat = new Label("Unbooked Seat");
        greenSeat.setLayoutY(160);
        greenSeat.setLayoutX(590);
        greenSeat.setStyle("-fx-font-size:14px;");
        Button unbooked = new Button("   ");
        unbooked.setLayoutY(160);
        unbooked.setLayoutX(560);
        unbooked.setStyle("-fx-background-color:green;");

        Label redSeat = new Label("Booked Seat");
        redSeat.setLayoutX(590);
        redSeat.setLayoutY(200);
        redSeat.setStyle("-fx-font-size:14px;");
        Button booked = new Button("   ");
        booked.setLayoutX(560);
        booked.setLayoutY(200);
        booked.setStyle("-fx-background-color:red;");


        GridPane gridPane = new GridPane();
        int columnIndex = 0;
        int rowIndex = 0;
        int rowIndexes = 0;

        int[] reservedSeats = new int[1];

        String seatNumber;
        for (int i = 1; i < (SEATING_CAPACITY + 1); i++) {
            if (i <= 9) {
                seatNumber = "0" + (i);
            } else {
                seatNumber = "" + (i);
            }
            seat[i] = new Button(seatNumber);
            gridPane.add(seat[i], columnIndex, rowIndex);
            columnIndex++;
            rowIndexes++;

            if (rowIndexes == 4) {
                columnIndex = 0;
                rowIndexes = 0;
                rowIndex++;
            }
        }
        for (int f = 1; f < (SEATING_CAPACITY + 1); f++) {
            if (seatBooking[f].equals("Empty")) {
                seat[f].setStyle("-fx-background-color: green;");
            }
            if (seatBooking[f].equals("booked")) {
                seat[f].setStyle("-fx-background-color: red");
            }
        }

        List<Integer> bookedCurrent = new ArrayList<>();
        for (int f = 1; f < (SEATING_CAPACITY + 1); f++) {
            int finalF = f;
            seat[f].setOnAction(event -> {
                seat[finalF].setStyle("-fx-background-color: red");
                seatBooking[finalF] = "booked";
                bookedCurrent.add(finalF);
            });

            submit.setOnAction(event -> {
                String personName = cusName.getText();
                personName = personName.toLowerCase();
                window.close();


                for ( int loopSeatArray = 1; loopSeatArray< (SEATING_CAPACITY + 1); loopSeatArray++) {
                    if (loopSeatArray == reservedSeats[0]) {
                        seatBooking[loopSeatArray] = "Booked";
                        customerName[loopSeatArray] = personName;
                    }
                    seatBookingAndCustomerName[loopSeatArray] = seatBooking[loopSeatArray];
                }
                for (int total = 43; total < (SEATING_CAPACITY + 1); total++){
                    seatBookingAndCustomerName[total]= customerName[total-42];                }
                displayMenu(seatBooking, customerName, seatBookingAndCustomerName);
            });
        }

        gridPane.setLayoutX(160);
        gridPane.setLayoutY(80);
        gridPane.setHgap(20);
        gridPane.setVgap(5);


        AnchorPane layout2 = new AnchorPane();
        layout2.setStyle("-fx-background-color:#5a89a3; ");
        layout2.getChildren().addAll(gridPane,submit,exit,header1,greenSeat,unbooked,redSeat,booked);

        scene2 = new Scene(layout2,800,600);
        window.setTitle("Train Booking System");
        window.show();
        window.setOnCloseRequest(event -> {
            window.close();
            displayMenu(seatBooking,customerName,seatBookingAndCustomerName);
        });

    }

and here is the part of the code where I prompt the user for the name and find the location of given name:这是我提示用户输入名称并找到给定名称位置的代码部分:

private void findCustomerSeats(String[] seatBooking, String[] customerName, String[] seatBookingAndCustomerName) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter customer name: ");
        String name = input.nextLine();
        boolean flag = false;

        for (int i = 1; i < (SEATING_CAPACITY + 1); i++){
            if (name.toLowerCase().equals(customerName[i])){
                System.out.println("Seats booked are: " + seatBooking);
                flag = true;
            }
        }
        if (flag !=true){
            System.out.println(name + " has not reserved a seat");
        }
        displayMenu(seatBooking,customerName,seatBookingAndCustomerName);
    }

when the above code is run, and when I input the name, it plain out does not work.当上面的代码运行时,当我输入名称时,它显然不起作用。

One potential issue, your for loop is starting at index 1 , I would begin by using something like this:一个潜在的问题是,您的for loopindex 1开始,我将首先使用以下内容:

for (int idx = 0; idx < customerName.length; idx++) {
//This way you will not check an index of your array that does not exist 
//which could cause a NullPointerException
//Additionally, this is useful if you want to grow your customerName array
}

Another useful tip, that I use all the time.另一个有用的提示,我一直在使用。 Try printing out more information and see if you can spot the issue.尝试打印出更多信息,看看是否能发现问题。 This might seem trivial but I have found it extremely helpful.这可能看起来微不足道,但我发现它非常有帮助。 For example:例如:

System.out.println("Inputted Name: " + name);
for (int idx = 0; idx < customerName.length; idx++) {
System.out.println("Does " + name.toLowerCase() + " = " + customerName[idx] + " ?");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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