簡體   English   中英

即使所有值都保存在數組中,為什么我的 find 方法在這里不起作用?

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

我對編程相當陌生,我似乎無法在我讓程序搜索特定關鍵字的區域中找到問題,但它已保存在數組中。

這是我添加客戶詳細信息的代碼:


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

    }

這是我提示用戶輸入名稱並找到給定名稱位置的代碼部分:

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

當上面的代碼運行時,當我輸入名稱時,它顯然不起作用。

一個潛在的問題是,您的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
}

另一個有用的提示,我一直在使用。 嘗試打印出更多信息,看看是否能發現問題。 這可能看起來微不足道,但我發現它非常有幫助。 例如:

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