2017-03-22 4 views
-2

*** Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0Setter/Getter und encapusulation JavaIndexoutbounds

in Print Personal Passanger bei ticketing.Ticketing.main (Ticketing.java:102) ***

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

was soll ich tun, wenn ich die Kapselung von Eingabebenutzer machen möchte.

+4

Mögliche Duplikat [Was bewirkt eine java.lang.ArrayIndexOutOfBoundsException und wie verhindere ich das?] (http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexcep das-und-wie-ich-verhindere-es) – azurefrog

Antwort

0

Sieht so aus, als hätten Sie viele Probleme mit Arrays, der angezeigte Code weist viele Inkonsistenzen auf. Versuchen Sie für das nächste Mal ein minimales, vollständiges und verifizierbares Beispiel, um bessere Hilfe zu erhalten. Hier können Sie die Richtlinien sehen, um eins zu erstellen: https://stackoverflow.com/help/mcve.

Auch ich empfehle Ihnen, Arrays und Variablen in Java zu studieren.

Ich habe Ihren Code so geändert, dass er auf diese Weise ausgeführt wird (er kann von Ihrem abweichen, weil ich nicht alles weiß, was Sie tun).

Hier ist Ihre modifizierte 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"); 
     } 
    } 
} 

Und diese Klasse:

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

} 

Die Ausgabe lautet:

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 [email protected] 


+++ +++ 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 [email protected]