如何在使用单独的过程时将数据存储到数组中?

时间:2017-03-09 17:50:05

标签: java arrays stored-procedures procedure

我一直在努力让我的程序按照我想要的方式工作。下面的代码并不完全有效,我不明白为什么,我也需要它作为一个程序,但我不知道如何解决这个问题。

以下是我需要的代码作为一个单独的程序,允许用户选择数组中的哪个房间并存储用户在菜单中输入“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'以允许用户使用set array为房间号添加名称。
  • 允许程序从指定的房间删除名称并清除数组中房间的索引

这是我无法解决将数据(Name)输入数组的问题,因为我不完全知道错误的含义: Image wit error

1 个答案:

答案 0 :(得分:0)

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

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

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

    }

}