Setter / Getter和encapusulation JavaIndexoutbounds

时间:2017-03-22 18:08:30

标签: java

***线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:0

在Print Personal Passanger中     在Ticketing.Ticketing.main(Ticketing.java:102)***

许多int = 0;

                    DataPenumpangPesawat name [];
                    name = new DataPenumpangPesawat [many];
                    DataPenumpangPesawat address [];
                    address = new DataPenumpangPesawat [many];
                    DataPenumpangPesawat sex [];
                    Gender = new DataPenumpangPesawat [many];
                    DataPenumpangPesawat ktp [];
                    ktp = new DataPenumpangPesawat [many];


                    System.out.print ( "Input Data Many Passenger =");
                    lot = scan.nextInt ();

                    // Array Request User Data Input Entering Aircraft
                    for (int i = 0; i <lot; i ++) {
                        System.out.print ( "\ nData to-" + (i + 1) + "\ n");
                        scan.nextLine ();
                        System.out.print ( "Passenger Name =");
                        // I have a problem in here
                        name [i] = new DataPenumpangPesawat ();
                        String name2 = scan.nextLine ();
                        name [i] .setNama (name2);
                        System.out.print ( "Address =");
                        address [i] = new DataPenumpangPesawat ();
                        String Address = scan.nextLine ();
                        Address [i] .setAlamat (Address);
                        System.out.print ( "Gender (1 = Male / 2 = Female) =");
                        sex [i] = new DataPenumpangPesawat ();
                        scan.nextInt gender = int ();
                        sex [i] .setJenKel (gender);
                        System.out.print ( "NIK KTP (NPM) =");
                        ktp [i] = new DataPenumpangPesawat ();
                        int KTPP = scan.nextInt ();
                        ktp [i] .setNik (KTPP);

                    }

                    for (int i = 0; i <lot; i ++) {
                        System.out.print ( "\ n \ n +++ +++ TICKET PRINT \ n");
                        System.out.print ( "Print Tickets to-" + (i + 1) + "\ n");
                        System.out.print ( "================= \ n");
                        // Print Name Aircraft
                        if (pil2 == 1) {
                            System.out.print ( "Garuda Indonesia plane = \ n");
                        } Else {
                            System.out.print ( "Aircraft = Batik Air \ n");
                        }
                        // Print Class Aircraft
                        if (pil3 == 1) {
                            System.out.print ( "Class = Executive \ n");
                        } Else {
                            System.out.print ( "Class = Economy \ n");
                        }
                        // Print Destination City Passenger
                        if (pil4 == 1) {
                            System.out.print ( "City of Interest = Yogyakarta \ n");
                        } Else if (pil4 == 2) {
                            System.out.print ( "City of Interest = Lombok \ n");
                        } Else {
                            System.out.print ( "City of Interest = Jayapura \ n");
                        }
                        // Print Departure Date
                        System.out.print ( "Jam Departure =" + date + "-" + Moon + "-" + year + "\ n");

                        // Print Jam and Jam Arrive Departures
                        if (piljam == 1) {
                            System.out.print ( "Jam Departure = 09:30 AM \ n");
                            System.out.print ( "Clock Arrive = 10:40 AM \ n");
                        } Else if (piljam == 2) {
                            System.out.print ( "Jam Departure = 11:50 AM \ n");
                            System.out.print ( "Clock Arrive = 13:20 AM \ n");
                        } Else {
                            System.out.print ( "Jam Departure = 3:15 PM \ n");
                            System.out.print ( "Clock Arrive = 5:40 PM \ n");
                        }
                        // Print Personal Data Passengers
                        String nama1 = name [i] .get`Nama ();
                        System.out.println ( "Name =" + nama1);
                        String address1 = address [i] .getAlamat ();
                        System.out.println ( "Address =" + address1);
                        int gender1 = sex [i] .getJenKel ();
                        System.out.println ( "Gender =" + address1);
                        System.out.print ( "NIK =" + ktp [i] + "\ n");

如果我想从输入用户进行封装,我该怎么办? Java

1 个答案:

答案 0 :(得分:0)

Looks that you have a lot of troubles with arrays, the code you show have a lot of inconsistencies, for next time try to provide a Minimal, Complete, and Verifiable example in order to get better help. Here you can see the guidelines to create one: https://stackoverflow.com/help/mcve.

Also I suggest you to study arrays and variables in java.

I modified your code to run in this way, (it may differ from yours because I don't know everything you are doing).

Here is your modified code:

import java.io.IOException;
import java.util.Scanner;

public class Example {

    public static void main(String[] args) throws IOException {

        Scanner scan = new Scanner(System.in);
        int many = 10;

        /**
         * I fix this variables and initialize them to run your program
         */
        DataPenumpangPesawat name[] = new DataPenumpangPesawat[many];
        DataPenumpangPesawat address[] = new DataPenumpangPesawat[many];
        DataPenumpangPesawat sex[] = new DataPenumpangPesawat[many];
        DataPenumpangPesawat[] Gender = new DataPenumpangPesawat[many]; // This variable is never used
        DataPenumpangPesawat ktp[];
        ktp = new DataPenumpangPesawat[many];

        System.out.print("Input Data Many Passenger =");
        int lot = scan.nextInt();

        // Array Request User Data Input Entering Aircraft
        for (int i = 0; i < lot; i++) {
            System.out.print("\n Data to-" + (i + 1) + "\n");
            scan.nextLine();
            System.out.print("Passenger Name =");
            // I have a problem in here
            name[i] = new DataPenumpangPesawat();
            String name2 = scan.nextLine();
            name[i].setNama(name2);
            System.out.print("Address =");
            address[i] = new DataPenumpangPesawat();
            String Address = scan.nextLine();
            address[i].setAlamat(Address);
            System.out.print("Gender (1 = Male / 2 = Female) =");
            sex[i] = new DataPenumpangPesawat();
            // scan.nextInt gender = int ();
            // sex[i].setJenKel (gender);
            System.out.print("NIK KTP (NPM) =");
            ktp[i] = new DataPenumpangPesawat();
            int KTPP = scan.nextInt();
            ktp[i].setNik(KTPP);

        }

        for (int i = 0; i < lot; i++) {
            System.out.print("\n \n +++ +++ TICKET PRINT \n");
            System.out.print("Print Tickets to-" + (i + 1) + "\n");
            System.out.print("================= \n");
            /**
             * I initialize this variables with 1 because I don't know where they comes! You must change the initialization for real values
             */
            int pil2 = 1;
            int pil3 = 1;
            int pil4 = 1;
            int piljam = 1;
            if (pil4 == 1) {
                // Print Name Aircraft
                if (pil2 == 1) {
                    System.out.print("Garuda Indonesia plane = \n");
                } else {
                    System.out.print("Aircraft = Batik Air \n");
                }
                // Print Class Aircraft
                if (pil3 == 1) {
                    System.out.print("Class = Executive \n");
                } else {
                    System.out.print("Class = Economy \n");
                }
                // Print Destination City Passenger
                System.out.print("City of Interest = Yogyakarta \n");
            } else if (pil4 == 2) {
                System.out.print("City of Interest = Lombok \n");
            } else {
                System.out.print("City of Interest = Jayapura \n");
            }
            // Print Departure Date
            // System.out.print ( "Jam Departure =" + date + "-" + Moon + "-" + year + "\ n"); I commented this line because I don't know where the
            // variables date and year are.
            System.out.print("Jam Departure = \n");

            // Print Jam and Jam Arrive Departures
            if (piljam == 1) {
                System.out.print("Jam Departure = 09:30 AM \n");
                System.out.print("Clock Arrive = 10:40 AM \n");
            } else if (piljam == 2) {
                System.out.print("Jam Departure = 11:50 AM \n");
                System.out.print("Clock Arrive = 13:20 AM \n");
            } else {
                System.out.print("Jam Departure = 3:15 PM \n");
                System.out.print("Clock Arrive = 5:40 PM \n");
            }
            // Print Personal Data Passengers
            String nama1 = name[i].getNama();
            System.out.println("Name =" + nama1);
            String address1 = address[i].getAlamat();
            System.out.println("Address =" + address1);
            int gender1 = sex[i].getJenKel();
            System.out.println("Gender =" + address1);
            System.out.print("NIK =" + ktp[i] + "\n");
        }
    }
}

And this class:

public class DataPenumpangPesawat {

    private String nama;
    private String alamat;
    private int kTPP;
    private int jenKel;
    private int nik;

    public int getNik() {
        return nik;
    }

    public void setNik(int nik) {
        this.nik = nik;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public String getAlamat() {
        return alamat;
    }

    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    public int getkTPP() {
        return kTPP;
    }

    public void setkTPP(int kTPP) {
        this.kTPP = kTPP;
    }

    public int getJenKel() {
        return jenKel;
    }

    public void setJenKel(int jenKel) {
        this.jenKel = jenKel;
    }

}

The output is:

Input Data Many Passenger =2

 Data to-1
Passenger Name =andres
Address =mexico
Gender (1 = Male / 2 = Female) =NIK KTP (NPM) =1

 Data to-2
Passenger Name =andy
Address =mex
Gender (1 = Male / 2 = Female) =NIK KTP (NPM) =1


 +++ +++ TICKET PRINT 
Print Tickets to-1
================= 
Garuda Indonesia plane = 
Class = Executive 
City of Interest = Yogyakarta 
Jam Departure = 
Jam Departure = 09:30 AM 
Clock Arrive = 10:40 AM 
Name =andres
Address =mexico
Gender =mexico
NIK =com.stackoverflow.sorting_array.DataPenumpangPesawat@28d93b30


 +++ +++ TICKET PRINT 
Print Tickets to-2
================= 
Garuda Indonesia plane = 
Class = Executive 
City of Interest = Yogyakarta 
Jam Departure = 
Jam Departure = 09:30 AM 
Clock Arrive = 10:40 AM 
Name =andy
Address =mex
Gender =mex
NIK =com.stackoverflow.sorting_array.DataPenumpangPesawat@1b6d3586