Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用单独的过程将数据存储到阵列中?_Java_Arrays_Stored Procedures_Procedure - Fatal编程技术网

Java 如何使用单独的过程将数据存储到阵列中?

Java 如何使用单独的过程将数据存储到阵列中?,java,arrays,stored-procedures,procedure,Java,Arrays,Stored Procedures,Procedure,我一直在努力让我的程序按我的意愿运行。下面的代码不能完全工作,我不明白为什么,我也需要它作为一个过程,但我不知道如何解决这个问题 以下是我需要的代码,作为一个单独的过程,允许用户选择阵列中的哪个房间,并存储用户在菜单中输入“a”后要输入的名称: System.out.println("Enter room number(0-9)"); roomNum =input.nextInt(); System.out.prin

我一直在努力让我的程序按我的意愿运行。下面的代码不能完全工作,我不明白为什么,我也需要它作为一个过程,但我不知道如何解决这个问题

以下是我需要的代码,作为一个单独的过程,允许用户选择阵列中的哪个房间,并存储用户在菜单中输入“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(" ");
以下是我的完整程序:

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


}
项目背景: 使用单独的程序生成酒店预订系统并传递参考

我想要完成的是:

允许程序接受输入“A”,以允许用户使用集合数组向房间号添加名称。 允许程序从指定房间中删除名称,并清除阵列中房间的索引 这是我无法解决的将数据名称输入数组的问题,因为我不知道错误的确切含义:

通过多种选择,您可以将需要模拟的内容拆开,在主环境之外进行检查,然后将其放回主项目中。我就是这么做的

在下面,您可以找到一个小程序来模拟从数组中添加和删除,从而可以解决您的问题。当然,它不处理某些情况,但作为一个基本程序,它允许在数组中添加和删除用户名

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

    }

}

最简单的方法是将公共变量定义为全局变量——在所有方法之外,这样所有方法都可以访问它们。只是一个小提示:您可能希望将鼠标悬停在这两个过程标记上,然后阅读它所说的内容。我不确定这些是否合适。你可能还想读一读:我相信你已经有了答案。。。你的初始化。。。方法就是做你想做的事。将数组传递给它,然后初始化填充它。Java中的对象参数是通过引用传递的。在旁注中,您应该使用for int x=0;x<酒店参考长度;这样,当数组大小更改时,您就不必重写代码,并避免异常