Geoddie
Geoddie

Reputation: 11

how to add a value of array for a specified index without replacing it?

THIS IS ONE METHOD CLASS ONLY .

        System.out.print("1. Add Customer..............................................P500.00\n");
        case 1://Add Customer

            for (int x = 0; x < CustomerInfo.length; x++){// Start For Records
                x++;
                System.out.print("\nConfimation Number: ");
                CustomerInfo[x][0][0][0][0][0] = br.readLine();
                System.out.print("\nFirst Name: ");
                CustomerInfo[x][1][0][0][0][0] = br.readLine();
                System.out.print("Last Name: ");
                CustomerInfo[x][0][1][0][0][0] = br.readLine();
                System.out.print("Guest: ");
                CustomerInfo[x][0][0][1][0][0] = br.readLine();
                System.out.print("Night: ");
                CustomerInfo[x][0][0][0][1][0] = br.readLine();
                System.out.print("Accommodation: ");
                CustomerInfo[x][0][0][0][0][1] = br.readLine();
                System.out.println();
                break;
            }

            break;


            for (int x = 0; x < CustomerInfo.length; x++){

                if (CustomerInfo[x][0][0][0][0][0] != null){
                if (CustomerInfo[x][1][0][0][0][0] != null){
                if (CustomerInfo[x][0][1][0][0][0] != null){
                if (CustomerInfo[x][0][0][1][0][0] != null){
                if (CustomerInfo[x][0][0][0][1][0] != null){
                if (CustomerInfo[x][0][0][0][0][1] != null){


                System.out.print("\n|---------------------------------------------------------------------|\n");
                System.out.print("\n\nConfirmation Number:      |--------------->  " +   CustomerInfo[x][0][0][0][0][1]);
                System.out.print("\nGuest Name:                 |--------------->  " + CustomerInfo[x][1][0][0][0][0] + " " + CustomerInfo[x][0][1][0][0][0]);
                System.out.print("\nNumber Of Guest:            |--------------->  " + CustomerInfo[x][0][0][1][0][0]);
                System.out.print("\nNumber Of Nights:           |--------------->  " + CustomerInfo[x][0][0][0][1][0]);
                System.out.println("\nAccommodations:               |--------------->  " + CustomerInfo[x][0][0][0][0][1]);

i add customer for first time then if i add customer for the second time the first customer wasn't recorded.

i noticed that the problem is the increment.

 for (int x = 0; x < CustomerInfo.length; x++) 

because i use break after that.

break;

but i want to accomplish is to add customer and when i add another one the previous should display along with the new.

what i asked is i want to add more array values one after one . or like after i add value to my '[x]' i want to add again, but not at the same time

any suggestions ?

i really need your help.

Upvotes: 0

Views: 51

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53535

You probably meant to declare CustomerInfo as follows:

String[] customerInfo = new String[6];

and have each array - hold the information of one customer, where first-name will be stored on the first index, last-name on the second and etc.

By doing something like CustomerInfo[][][][][][] - the code tries to declare/access a multidimensional (6-dimensional) array - which is probably not what you wanted.

Now Java is an object oriented programming language, so instead of multidimensional arrays you should try to design your code to use objects - it will make your code easier to read and maintain.

So first, create a CustomerInfo class:

class CustomerInfo {
    String confirmationNumber;
    String firstName;
    String lastName;
    String guest;
    String night;
    String accommodation;

    public CustomerInfo(String confirmationNumber,
                        String firstName,
                        String lastName,
                        String guest,
                        String night,
                        String accommodation) {
        this.confirmationNumber = confirmationNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.guest = guest;
        this.night = night;
        this.accommodation = accommodation;
    }
}

and now that you have such a "holder" for the data, you can read the user input and save it into a list of CustomerInfo:

public static void main(String[] args) {
    ...
    List<CustomerInfo> customerInfos = new ArrayList<CustomerInfo>();
    for (int i = 0; i < customerInfos.length; i++){// Start For Records
        System.out.print("\Confirmation Number: ");
        String confirmationNumber = br.readLine();
        System.out.print("\nFirst Name: ");
        String firstName = br.readLine();
        System.out.print("Last Name: ");
        String lastName = br.readLine();
        System.out.print("Guest: ");
        String guest = br.readLine();
        System.out.print("Night: ");
        String night = br.readLine();
        System.out.print("Accommodation: ");
        String accommodation = br.readLine();
        CustomerInfo customer = new CustomerInfo(confirmationNumber, firstName, lastName, guest, night, accommodation);
        customerInfos.add(customer);
        System.out.println();
    }
}

and if you want to do even better, add some validations on top, for example, if guest is a number, you can read it as int or try to parse it as an int - and if the user entered something weird - display a proper error message. And etc.

Upvotes: 0

Afsin Buyuksarac
Afsin Buyuksarac

Reputation: 298

    case 1://Add Customer

        for (int x = 0; x < CustomerInfo.length; x++){// Start For Records
            **x++;**

You have problem with your loop. You increase your incremental variable when you start loop and when you finish loop two times.

That means you start not with 0 but with 1. But you want to check that if there is a customer at 0.

Upvotes: 0

Related Questions