0

I have some problem with my java coding as I'm new for java programming. I try to write the program to create the row with the information, but each row has some difference a few values.

I try to use array but I always got error as below. I try to research on internet by myself, but I still could not solve my issue. Do you have any idea about what I wrong? Thanks a lot in advance for every answer.

My output:

Enter the MSISDN that you need 9 digits (Ex:660000204) or 0 for exit 660000204 Enter the total number that you need 1 to 100 5 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 660000204 at myWork.genBulkFile.main(genBulkFile.java:74)


public static void main(String args[]) {


    Date today = new Date();
    SimpleDateFormat formattedDate = new SimpleDateFormat("ddMMyyyy");
    Calendar cA = Calendar.getInstance();
    cA.add(Calendar.YEAR, -4); // For 4 years old
    Calendar cB = Calendar.getInstance();
    cB.add(Calendar.YEAR, -5); // For 5 years old
    Calendar cC = Calendar.getInstance();
    cC.add(Calendar.YEAR, -6); // For 6 years old
    Calendar c1 = Calendar.getInstance();
    c1.add(Calendar.YEAR, -17); // For 17 years old
    Calendar c2 = Calendar.getInstance();
    c2.add(Calendar.YEAR, -18); // For 18 years old
    Calendar c3 = Calendar.getInstance();
    c3.add(Calendar.YEAR, -19); // For 19 years old
    Calendar c4 = Calendar.getInstance();
    c4.add(Calendar.YEAR, -20); // For 20 years old
    Calendar c5 = Calendar.getInstance();
    c5.add(Calendar.YEAR, -21); // For 21 years old
    Calendar c6 = Calendar.getInstance();
    c6.add(Calendar.YEAR, -22); // For 22 years old
    String birthDay4 = (String) (formattedDate.format(cA.getTime()));
    String birthDay5 = (String) (formattedDate.format(cB.getTime()));
    String birthDay6 = (String) (formattedDate.format(cC.getTime()));
    String birthDay17 = (String) (formattedDate.format(c1.getTime()));
    String birthDay18 = (String) (formattedDate.format(c2.getTime()));
    String birthDay19 = (String) (formattedDate.format(c3.getTime()));
    String birthDay20 = (String) (formattedDate.format(c4.getTime()));
    String birthDay21 = (String) (formattedDate.format(c5.getTime()));
    String birthDay22 = (String) (formattedDate.format(c6.getTime()));

    for (;;) {

        System.out.println("Enter the MSISDN that you need 6 digits (Ex:660000204) or 0 for exit");
        @SuppressWarnings("resource")
        Scanner pf1 = new Scanner(System.in);
        while (!pf1.hasNextInt()) {
            System.out.println("Invalid input, please enter the number 9 digits:");
            pf1.next();
        }

        int startMsisdn = pf1.nextInt();
        if (startMsisdn == 0) {
            System.out.println("Exit program" + "\n");
            System.exit(0);
        } else if (startMsisdn > 0) {
            System.out.println("Enter the total number that you need 1 to 100");
            @SuppressWarnings("resource")
            Scanner nb1 = new Scanner(System.in);
            while (!nb1.hasNextInt()) {
                System.out.println("Invalid input, please enter the number:");
                nb1.next();
            }
            int totalNumber = nb1.nextInt();
            if (totalNumber == 0) {
                System.out.println("Exit program" + "\n");
                System.exit(0);
            } else if (totalNumber > 0) {
                int endMsisdn = startMsisdn+totalNumber;
                int[] MsisdnArray = new int [totalNumber];
                //System.out.println("Array size is " + MsisdnArray.length);
                for (int i = startMsisdn; i < endMsisdn; i++){
                MsisdnArray[i] = startMsisdn++;
                    }
                System.out.println("ked1," + MsisdnArray[1] + ",123833" + MsisdnArray[1] + "," + MsisdnArray[1]
                        + ",I,firstIndivBirth,lastIndivBirth,F," + birthDay4
                        + ",L,East,Batouri,Batouri Kako - Alliance Voyage,Birthcertificate,birthnoxx,10102017,,nationalid,122333445,10092018,tutorFirstIndiv,tutorLastIndiv,F,birth@gmail.com,1234567,BICEC,S,E,MU,PrepaidGSM,MTN,,,,,,,,,,,");
                System.out.println("ked1," + MsisdnArray[2] + ",123833" + MsisdnArray[2] + "," + MsisdnArray[2]
                        + ",I,firstIndivBirth,lastIndivBirth,F," + birthDay5
                        + ",L,East,Batouri,Batouri Kako - Alliance Voyage,Birthcertificate,birthnoxx,10102017,,nationalid,122333445,10092018,tutorFirstIndiv,tutorLastIndiv,F,birth@gmail.com,1234567,BICEC,S,E,MU,PrepaidGSM,MTN,,,,,,,,,,,");
                System.out.println("ked1," + MsisdnArray[3] + ",123833" + MsisdnArray[3] + "," + MsisdnArray[3]
                        + ",I,firstIndivBirth,lastIndivBirth,F," + birthDay6
                        + ",L,East,Batouri,Batouri Kako - Alliance Voyage,Birthcertificate,birthnoxx,10102017,,nationalid,122333445,10092018,tutorFirstIndiv,tutorLastIndiv,F,birth@gmail.com,1234567,BICEC,S,E,MU,PrepaidGSM,MTN,,,,,,,,,,,");


        }
    }
}

}}

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • and where is line 74 ? – azro Jul 16 '18 at 11:40
  • Please try debugging your code. – Prashant Jul 16 '18 at 11:40
  • Your `startMsisdn` values `660000204`, and you assign `i` to its value, but you don't have an array as large, the array's dimension does not change, go from `0` to its size for (int i = 0; i < MsisdnArray.length; i++){ MsisdnArray[i] = startMsisdn++; } – azro Jul 16 '18 at 11:43
  • @arzo: Thanks a lot for your help. I tried something similar as your suggestion before, but it never work. I will try more. Finally, it works fine now. Thanks a lot again. – sumate dosnan Jul 16 '18 at 15:26

0 Answers0