1

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)

kenlukas
  • 3,616
  • 9
  • 25
  • 36
  • How many number are you willing to take as input? Your program will keep running and will never reach the point you are printing the result because of the while loop `while(sc.hasNextInt())` it will always keep waiting for input – Mohamed Ibrahim Elsayed Nov 14 '18 at 00:32

2 Answers2

0

The reason is with this line while (IsRepeating(num)){. Inside the loop you don't change the num so if it is a repeating, it will always be. This line loops (and increasing count) while num is repeating, i.e. while forever.

You should change this into if (IsRepeating(num)){

Mr.K
  • 559
  • 4
  • 9
  • What OP asked is "Any idea how can i fix this error?", I'm pointing out the problem of this code, not compiling it. OP should know which line he is getting the error already if he has a compiler. – Mr.K Nov 14 '18 at 00:31
  • Woooow! That solved my problem but at the same time now i have a result like 11000000000 if i enter 11 as an input. DO you know how can i make other zeros disappeared? – Rodion Raskolnikov Nov 14 '18 at 00:33
  • i mean java creates zeros for the rest of my array. I dont want them to be seen when i print my array. any solution for that? – Rodion Raskolnikov Nov 14 '18 at 00:37
  • If you just don't want them to be seen, just filter them out with something like `if (arr[i] > 0) System.out.println(arr[i]);` – Mr.K Nov 14 '18 at 00:38
-1

Your var count is the problem, you're initializing the array to a size of 5 elements, and at some point you're giving count > 4.

Sergio Flores
  • 439
  • 2
  • 8