0

Below is the code for a method I have in my program

private static void findRoom(String[] hotel) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Press Q to exit program and W to go back to menu");
    System.out.println("Enter name of customer that needs to be found");
    String name = sc.nextLine();
    if (name.equalsIgnoreCase("q")){
        System.exit(0);
    }else if(name.equalsIgnoreCase("w")){
        return;
    }
    boolean found = false;
    for (int i = 1; i < hotel.length; i++) {
        if (hotel[i].equalsIgnoreCase(name)) {
            System.out.println("Room number of " + name + " is " + i);
            found = true;
            break;
        }
    }
    if (found == false) {
        System.out.println("Such a customer does not exist. Please enter valid name");
    }
}

In the 8th line what I want to do is if the user inputs the letter "w" or "W" then the program should exit the method and go back to the main method. But instead of returning what my program does is start the method all over again

Here is what comes up on the console :

Press Q to exit program and W to go back to menu Enter name of customer that needs to be found w Press Q to exit program and W to go back to menu Enter name of customer that needs to be found w Press Q to exit program and W to go back to menu Enter name of customer that needs to be found

Like this it runs forever

How do I make the program return to the main method?

Below is the main method

public static void main(String[] args) throws IOException {
    String roomName = null;
    int roomNum = 0;
    Scanner sc = new Scanner(System.in);
    String[] hotel = new String[11];
    initialise(hotel);
    System.out.println("Select option: ");
    System.out.println("E: Display Empty rooms");
    System.out.println("D: Delete customer from room");
    System.out.println("F: Find room from customer name");
    System.out
            .println("S: Store program array data into a plain text file");
    System.out
            .println("L: Load program data back from the file into the array");
    System.out.println("O: View rooms Ordered alphabetically by name.");
    System.out.println("Q : Exit program");
    String input = sc.next();
    while (true) {
        if ((input.equals("Q")) || (input.equals("q"))) {
            System.exit(0);
        }
        if ((input.equals("E")) || (input.equals("e"))) {
            displayEmpty(hotel);
        }
        if ((input.equals("D")) || (input.equals("d"))) {
            deleteCustomer(hotel);
        }
        if ((input.equals("F")) || (input.equals("f"))) {
            findRoom(hotel);
        }
        if ((input.equals("S")) || (input.equals("s"))) {
            storeArray(hotel);
        }
        if ((input.equals("L")) || (input.equals("l"))) {
            loadArray(hotel);
        }
        if ((input.equals("O")) || (input.equals("o"))) {
            hotel = sortArray(hotel);
            veiwRooms(hotel);
        }
        if ((input.equals("A")) || (input.equals("a"))) {
            addCustomer(hotel, roomNum, roomName);
        }
        if ((input.equals("V")) || (input.equals("v"))) {
            veiwRooms(hotel);
        }
    }
}

Okay I found the solution. Changing the position of the while(true) statement worked

0 Answers0