Gangadhar B
Gangadhar B

Reputation: 109

ArrayIndexOutOfBoundsException coming in this array using java?

I have an array of numbers in Java and need to output the ones that consist of only duplicated digits. However, my code throws an ArrayIndexOutOfBoundsException. Where is the problem?

int[] inputValues= {122, 2, 22, 11, 234, 333, 000, 5555, 8, 9, 99};

for (int i = 0; i < inputValues.length; i++) {
    int numberLength = Integer.toString(inputValues[i]).length();
//  System.out.println(numberLength);
    if (numberLength > 1) { //more than one digit in the number
        String s1 = Integer.toString(inputValues[i]);
        String[] numberDigits = s1.split("");

        for (int j = 1, k = 1; j < numberDigits.length; k++) {
            if (numberDigits[j].equals(numberDigits[k + 1])) {
                System.out.println("Duplicate values are:");
                //I need to print 22,11,333,000,5555,99 
            }
        }
    }
}

Upvotes: 0

Views: 124

Answers (6)

Sujit Thombare
Sujit Thombare

Reputation: 13

public static void  main(String[] args) {


    int[] inputValues={122,2,22,11,234,333,000,5555,8,9,99,1000};



    System.out.println("Duplicate values are:");
      for (int i = 0; i < inputValues.length; i++) {

          String strNumber = new Integer(inputValues[i]).toString();// get string from array

          if(strNumber.length()>1)  // string length must be greater than 1
          {

              Character firstchar =strNumber.charAt(0);  //get first char of string

              String strchker =strNumber.replaceAll(firstchar.toString(), ""); //repalce it with black



              if(strchker.length()==0)   // for duplicate values length must be 0
              {
                  System.out.println(strNumber);

              }

          }


       }


      /*
       * output will be 
       * Duplicate values are:
        22
        11
        333
        5555
        99
       * 
       * 
       */
}

This is what you want.....

Upvotes: 1

Shubhang Malviya
Shubhang Malviya

Reputation: 1635

Sorry for joining the party late. I think following is the piece of code you’re are looking for

private int[] getDuplicate(int[] arr) {
        ArrayList<Integer> duplicate = new ArrayList<Integer>();

        for (int item : arr) {
            if(item > 9 && areDigitsSame(item)) {
                duplicate.add(item);
            }
        }

        int duplicateDigits[] = new int[duplicate.size()];

        int index = 0;
        for (Integer integer : duplicate) {
            duplicateDigits[index ++] = integer;
        }
        return duplicateDigits;
    }

    public boolean areDigitsSame(int item) {
        int num = item;
        int previousDigit = item % 10;
        while (num != 0) {
            int digit = num % 10;
            if (previousDigit != digit) {
                return false;
            }
            num /= 10;
        }

        return true;
    }

Now , use it as below

int[]inputValues={122,2,22,11,234,333,000,5555,8,9,99};
int[] duplicates = getDuplicate(inputValues);

That's all

Enjoy!

Upvotes: 1

gaurs
gaurs

Reputation: 605

@ line 13: the s1.split("") results to [, 1, 2, 2] for 122 . Hence your numberDigits.length is 4. The loop runs from j = 1 to 3 ( j < numberDigits.length); hence the numberDigits[k + 1 ] is evaluated for index 4 which is unavailable for [, 1, 2, 2].

Another point is worth noting is int[]inputValues will always store 000 as 0 only.

The below mentioned method will take a integer number and will return true and false based on your requirement. It will use xor operator to check repetitive digits.

private static boolean exorEveryCharacter(int currentValue) {
        int result = 0;
        int previousNumber = -1;

        while (currentValue != 0) {
            int currentNumber = currentValue % 10;

            if(previousNumber == -1){
                previousNumber = currentNumber;
            }
            else{
                result = previousNumber ^ currentNumber;
            }

            currentValue /= 10;
        }
        return result == 0;
    }

Upvotes: 0

Sujit Thombare
Sujit Thombare

Reputation: 13

    public static void  main(String[] args) {


        String[] inputValues={"122","2","22","11","234","333","000","5555","8","9","99"};



        System.out.println("Duplicate values are:");
          for (int i = 0; i < inputValues.length; i++) {

              String strNumber = inputValues[i];// get string from array
              if(strNumber.length()>1)  // string length must be greater than 1
              {
                  Character firstchar =strNumber.charAt(0);  //get first char of string

                  String strchker =strNumber.replaceAll(firstchar.toString(), "0"); //repalce it with 0

                if(Integer.parseInt(strchker)==0)   //if all values are duplictae than result string must be 0
                {
                  System.out.println(strNumber);
                }

              }


           }

    }
   // /// result will be
 /*  Duplicate values are:
   22 
   11
   333
   000
   5555
   99
*/

if you want int array then you will not able to get "000" as duplicate value.

Upvotes: 0

Tushar Nallan
Tushar Nallan

Reputation: 784

This line is the culprit here -

for (int j = 1, k = 1; j < numberDigits.length; k++) {
    if (numberDigits[j].equals(numberDigits[k + 1])) {
        System.out.println("Duplicate values are:");//i need to print 22,11,333,000,5555,99,etc.
    }
}

The loop has a condition that's always true as value of j is always 1. Since k keeps on increasing by 1 for each iteration ( which are infinite btw ), the index goes out of array bounds.

Try -

for (int j = 0, k = 1; k < numberDigits.length; k++) {
    boolean isDuplicate = true;
    if (!numberDigits[j].equals(numberDigits[k])) {
        isDuplicate = false;
        break;
    }
}
if( isDuplicate ) {
    System.out.println("Duplicate values are:"+inputValues[i]);
}

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

There is no condition to stop the inner loop when k gets too big. j never changes in the inner loop, so j < numberDigits.length will either always be true or always be false.

Upvotes: 3

Related Questions