I need to print numbers who has repeating digits like 11 or 121. However, it gives an error when I give some input like 22
. I can't understand why I'm getting the error message. Any idea how can I fix this error?
import java.util.Scanner;
public class IdenticalNumbers {
public static void main(String[] args)
{
// Declare an object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
int max = 0;
int[] arr = new int[5];
int count =0;
// Check if an int value is available
while (sc.hasNextInt())
{
// Read an int value
int num = sc.nextInt();
while (IsRepeating(num)){
arr[count] = num;
count += 1;
}
if (num > max){
max = num;
}
}
System.out.println("Maximum integer is: " + max);
System.out.println("Numbers with identical digits are: ");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
sc.close();
}
public static boolean IsRepeating(int number)
{
String textual = "" + number;
for (int i = 0; i < textual.length(); i++)
{
for (int j = i + 1; j < textual.length(); j++)
{
if (textual.charAt(i) == textual.charAt(j))
return true;
}
}
return false;
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at IdenticalNumbers.main(IdenticalNumbers.java:23)