-1

I need to get from the user the length and the value of the array and return the digit that shows the most times. for example: the user gave me the length "4" and the numbers {83,238,8,54} the function will return " the numbers that shows the most times is 8"; but it send an error massage if the user enter more than 10 digits in general.

        String sum = "";
        
            // get the length 
                  System.out.println("Enter the size of the array :");
                  int size = in.nextInt();
             // create the array 
                  String[] arr = new String[size+1];
                  
             // get value 
                  System.out.println("Enter the elements of the array:(max capity 10 digits) ");
                  for(int i=0; i<arr.length; i++) {
                     arr[i] = in.nextLine();
                     sum+=arr[i];
                    
                  }
             
                      
                System.out.println("the numbers that you gave me are: "+sum);   
                int all = Integer.parseInt(sum);  
                
                System.out.println("the digit that shows the most time is: "+maxOccurring(all));
                  
    }

        static int countOccurrences(int x,int d) {

            int count = 0;
            while (x > 0)
            {

                if (x % 10 == d)
                count++;
                x = x / 10;
            }
            return count;
        }
         

        static int maxOccurring( int x)
        {
             

        if (x < 0)
            x = -x;

        int result = 0;

        int max_count = 1;

        for (int d = 0; d <= 10; d++)
        {

            int count = countOccurrences(x, d);
         
           
            if (count >= max_count)
            {
                max_count = count;
                result = d;
            }
        }
        return result;
        

    }
    }
    ```
Refael
  • 1
  • 1

1 Answers1

0

You get an NumberFormatException at the following line:

int all = Integer.parseInt(sum);

Exception trace is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "25445648942"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at scan.ScannerTest.main(ScannerTest.java:27)

This exception is telling you that Integer.parseInt can't interpret the number you're passing through it (obtained by concatenating all the entered digits), in this precise example: "25445648942"

That's because this function is trying to yield an Integer from all those digits, which has a maximum capacity of 2^31 - 1 = 2,147,483,647. The entered number is higher that this, so it logically fails.

You should treat each entered number separately and count the digits in each, then do a sum of all to get your answer.

BTW, you don't ever need to convert to a number type to count the digits. It's simple string processing

julien.giband
  • 2,467
  • 1
  • 12
  • 19