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

huangapple 未分类评论44阅读模式
英文:

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) {
    // ... (code for adding customer details)
}

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: " + seatBookingAndCustomerName[i]);
            flag = true;
        }
    }
    if (!flag) {
        System.out.println(name + " has not reserved a seat");
    }
    displayMenu(seatBooking, customerName, seatBookingAndCustomerName);
}

Please note that the provided code snippets are part of a larger program and rely on variables and methods that are not shown in the provided snippets. If you encounter issues with the code not working as expected, there might be additional factors at play beyond the code snippets provided.

英文:

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(&quot; Click to Book Seats &quot;);
        selectSeat.setLayoutX(320);
        selectSeat.setLayoutY(512);
        selectSeat.setOnAction(event -&gt; {
            window.setScene(scene2);
        });

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

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



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

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


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

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



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


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



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

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

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

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


        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 &lt; (SEATING_CAPACITY + 1); i++) {
            if (i &lt;= 9) {
                seatNumber = &quot;0&quot; + (i);
            } else {
                seatNumber = &quot;&quot; + (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 &lt; (SEATING_CAPACITY + 1); f++) {
            if (seatBooking[f].equals(&quot;Empty&quot;)) {
                seat[f].setStyle(&quot;-fx-background-color: green;&quot;);
            }
            if (seatBooking[f].equals(&quot;booked&quot;)) {
                seat[f].setStyle(&quot;-fx-background-color: red&quot;);
            }
        }

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

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


                for ( int loopSeatArray = 1; loopSeatArray&lt; (SEATING_CAPACITY + 1); loopSeatArray++) {
                    if (loopSeatArray == reservedSeats[0]) {
                        seatBooking[loopSeatArray] = &quot;Booked&quot;;
                        customerName[loopSeatArray] = personName;
                    }
                    seatBookingAndCustomerName[loopSeatArray] = seatBooking[loopSeatArray];
                }
                for (int total = 43; total &lt; (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(&quot;-fx-background-color:#5a89a3; &quot;);
        layout2.getChildren().addAll(gridPane,submit,exit,header1,greenSeat,unbooked,redSeat,booked);

        scene2 = new Scene(layout2,800,600);
        window.setTitle(&quot;Train Booking System&quot;);
        window.show();
        window.setOnCloseRequest(event -&gt; {
            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(&quot;Please enter customer name: &quot;);
        String name = input.nextLine();
        boolean flag = false;

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

when the above code is run, and when I input the name, it plain out does not work.

答案1

得分: 0

One potential issue, your for loop is starting at index 1, I would begin by using something like this:

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] + " ?");
}
英文:

One potential issue, your for loop is starting at index 1, I would begin by using something like this:

for (int idx = 0; idx &lt; 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(&quot;Inputted Name: &quot; + name);
for (int idx = 0; idx &lt; customerName.length; idx++) {
System.out.println(&quot;Does &quot; + name.toLowerCase() + &quot; = &quot; + customerName[idx] + &quot; ?&quot;);
}

huangapple
  • 本文由 发表于 2020年4月10日 08:13:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61132211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定