Search code examples
javaarraysstored-proceduresprocedure

How can I store data into an array whilst using a seperate procedure?


I've been struggling in order to get my program to work how I want it. This code down below doesn't fully work and I don't understand why, also I need it as a procedure but I do not know how to solve this issue out.

Here is the code I need as a separate procedure that allows the user to select which room within the array and store the name which is to be inputted after the user has entered 'A' in the menu:

System.out.println("Enter room number(0-9)");
                    roomNum =input.nextInt();
                    System.out.println("Enter name for room " + roomNum + ": " ) ;
                    Client = input.next();
                    hotel[roomNum] = Client;
                    add(hotel, Client);
                    System.out.println(" ");

Here is my complete program:

package hotel_program;

import java.util.*;




public class Hotel_Program {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String roomID;
    String[] hotel = new String[10];
    int roomNum = 0;
    char selection;
    boolean run = true;
    String Client;

    for(int i=0;i<hotel.length;i++)
    {
        hotel[i] = "e";
    }
// Displays the menu
    while (run) {
        System.out.println("---- HOTEL MENU ----");
        System.out.println("[E] Display Empty Rooms");
        System.out.println("[A] Add Customer");
        System.out.println("[V] View All Rooms");
        System.out.println("[D] Delete Customer From Room");
        System.out.println("[F] Find Room For Customer Name");
        System.out.println("[S] Save Data Input");
        System.out.println("[L] Load Data");
        System.out.println("[O] View Rooms");
        System.out.println("--------------------");
        selection = getInput();

        switch (selection) {
// Shows rooms that are empty (not occupied)
            case 'E':
                    for (int x = 0; x < hotel.length; x++) 
                    {

                        if (hotel[x].equals("e") || hotel[x].equals("E")) 
                        {
                        System.out.println("room " + x + " is empty");
                        }
                    }

                break;
// Allows user to add in client name and assign to room number
            case 'A':
                    System.out.println("Enter room number(0-9)");
                    roomNum =input.nextInt();
                    System.out.println("Enter name for room " + roomNum + ": " ) ;
                    Client = input.next();
                    hotel[roomNum] = Client;
                    add(hotel, Client);
                    System.out.println(" ");

                break;

            case 'V':

                break;

            case 'D':

                break;

            case 'F':

                break;

            case 'S':

                break;

            case 'L':

                break;

            case 'O':

                break;

        }

    }

}
private static char getInput() 
{
    Scanner scanner = new Scanner(System.in);
    String input = scanner.next();
    return input.charAt(0);
}

private static void initialise(String hotelRef[]) 
{
    for (int x = 0; x < 10; x++) {
        hotelRef[x] = "e";
    }
    System.out.println("initilise ");
}


}

Context of Project: To produce a hotel booking system using separate procedures and pass through reference.

What I want to accomplish:

  • Allow the program to accept the input 'A' to allow the user to add a name to the room number using the set array.
  • Allow the program to delete the name from the specified room and clears the index of the room within the array

This is the issue I can't resolve for inputting data (Name) into the array as I do not exactly know the meaning of the error: Image wit error


Solution

  • With multiple choices you could just take apart what you need to simulate, check it outside your main environment and then put it back into the main project. This is what I do.

    Underneath you can find a small program to simulate adding and deleting from your array, thus allowing you to solve your question. Sure it does not handle some cases but as a "basic program" it allows to ADD and REMOVE user names from an array.

    import java.util.Scanner;
    
    public class HotelBooking {
    
        Scanner in;
        String userInput;
        String userName;
    
        String[] hotelList;
        int i, progressiveNumber, repeat;
    
        HotelBooking() {
    
            progressiveNumber = 0; // fills the array or removes from it
            repeat = 0; // repeats this loop to check if it works
            hotelList = new String[10]; // creates a 10 nr spaced  array
    
            while (repeat < 5) {  // repeats 5 times to check if it is working
                in = new Scanner(System.in);
    
                System.out.print("Enter choice (in this case a to add or b to remove): ");
                userInput = in.next();
    
                addToArray(); // add names to array. It does not handle if user from begining choses b. it is just a demo afterall
                removeFromArray(); // removes from array
    
            }
    
        }
    
        private void addToArray() {
            // METHOD TO ADD STRING TO ARRAY
            if (userInput.equals("a")) {// handles only a. You could add "A" or whatever
                System.out.print("Enter name to insert: ");
                userName = in.next();
    
                for (i = 0; i < hotelList.length; i++) {
                    hotelList[progressiveNumber] = userName;
                }
                progressiveNumber++;
    
                System.out.print("Printing the hotel list: ");
                for (String scan : hotelList) {
                    System.out.print(scan + " ");
                }
                System.out.println();
                repeat++;
            }
        }
    
        private void removeFromArray() {
            // METHOD TO REMOVE STRING FROM ARRAY
            if (userInput.equals("b")) { // handles only b. You could add "B" or whatever
                System.out.print("Enter name to remove: ");
                userName = "";
                progressiveNumber--;
    
                for (i = 0; i < hotelList.length; i++) {
                    hotelList[progressiveNumber] = userName;
                }
    
                System.out.print("Printing the hotel list: ");
                for (String scan : hotelList) {
                    System.out.print(scan + " ");
                }
                System.out.println();
                repeat++;
            }
        }
    
        public static void main(String[] args) {
    
            new HotelBooking();
    
        }
    
    }