0

I have made this program using array concept in java. I am getting Exception as ArrayIndexOutOfBound while trying to generate product.

I made the function generateFNos(int max) to generate factors of the given number. For example a number 6 will have factors 1,2,3,6. Now,i tried to combine the first and the last digit so that the product becomes equal to 6. I have not used the logic of finding the smallest number in that array right now. I will do it later.

Question is Why i am getting Exception as ArrayIndexOutOfBound? [i couldn't figure out]

Below is my code

public class SmallestNoProduct {

    public static void generateFNos(int max) {
        int ar[] = new int[max];
        int k = 0;
        for (int i = 1; i <= max; i++) {
            if (max % i == 0) {
                ar[k] = i;
                k++;
            }
        }
        smallestNoProduct(ar);
    }

    public static void smallestNoProduct(int x[]) {
        int j[] = new int[x.length];
        int p = x.length;
        for (int d = 0; d < p / 2;) {
            String t = x[d++] + "" + x[p--];
            int i = Integer.parseInt(t);
            j[d] = i;
        }
        for (int u = 0; u < j.length; u++) {
            System.out.println(j[u]);
        }
    }

    public static void main(String s[]) {
        generateFNos(6);
    }
}
****OutputShown****

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at SmallestNoProduct.smallestNoProduct(SmallestNoProduct.java:36)
    at SmallestNoProduct.generateFNos(SmallestNoProduct.java:27)
    at SmallestNoProduct.main(SmallestNoProduct.java:52)

@Edit

The improved Code using array only.

public class SmallestNoProduct {
    public static void generateFNos(int max) {
        int s = 0;
        int ar[] = new int[max];
        int k = 0;
        for (int i = 1; i <= max; i++) {
            if (max % i == 0) {
                ar[k] = i;
                k++;
                s++;
            }
        }
        for (int g = 0; g < s; g++) {
            System.out.println(ar[g]);
        }
        smallestNoProduct(ar, s);
    }

    public static void smallestNoProduct(int x[], int s) {
        int j[] = new int[x.length];

        int p = s - 1;
        for (int d = 0; d < p;) {
            String t = x[d++] + "" + x[p--];
            System.out.println(t);

            int i = Integer.parseInt(t);
            j[d] = i;
        }
        /*for (int u = 0; u < j.length; u++) {
            System.out.println(j[u]);
        }*/
    }

    public static void main(String s[]) {
        generateFNos(6);
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55

4 Answers4

3

Maybe it better:

public class SmallestNoProduct {

    public static int smallest(int n) {
        int small = n*n;
        for(int i = 1; i < Math.sqrt(n); i++) {
            if(n%i == 0) {
                int temp = Integer.parseInt(""+i+""+n/i);
                int temp2 = Integer.parseInt(""+n/i+""+i);
                temp = temp2 < temp? temp2: temp;
                if(temp < small) {
                    small = temp;
                }
            }
        }
        return small;
    }

    public static void main(String[] args) {
        System.out.println(smallest(6));  //6
        System.out.println(smallest(10)); //25
        System.out.println(smallest(100)); //205
    }

}
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Yes,Its a great Code.Grateful to u for enriching my knowledge.Please Guide How to write code like yours it would be of great help. – mansi kbchawla Jun 11 '15 at 06:16
  • @mansi kbchawla factor a number n to 2 num(a & b), so (a, b) <=> (b, a). computer prime always use the range of 2~sqrt(n), it not contains 1. then I use range [1, sqrt(n)]. – Peter Pan Jun 11 '15 at 06:24
  • Sorry Could not understand Please Explain Again with example:) – mansi kbchawla Jun 11 '15 at 07:33
  • 1
    For `100` I can surely say that `250` is not the right answer. `205` (20 * 5) is smaller. – Ambrish Jun 11 '15 at 07:37
  • 1
    pair (a, b) => a*b is equals (b, a) => b*a, example factor 12 => 1,2,3,4,6,12. (1,12) equals (12, 1). so i must lt sqrt(n) in case of compute prime. – Peter Pan Jun 11 '15 at 07:38
  • @code-panda So,I assume that this was ur thought process in getting to the solution.Or if not then How did u get to the solution. – mansi kbchawla Jun 13 '15 at 02:12
2

Problem lies in this line

String t=x[d++]+""+x[p--];

x[p--] will try to fetch 7th position value, as p is length of array x i.e. 6 which results in ArrayIndexOutOfBound exception. Array index starts from 0, so max position is 5 and not 6.

You can refer this question regarding postfix expression.

Note: I haven't checked your logic, this answer is only to point out the cause of exception.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
2

We are unnecessarily using array here...

below method should work....

public int getSmallerMultiplier(int n)
{
  if(n >0 && n <10) // if n is 6
    return (1*10+n); // it will be always (1*10+6) - we cannot find smallest number than this
  else
  {
     int number =10;
     while(true)
     {
         //loop throuogh the digits of n and check for their multiplication
         number++;
     }
  }
}
Pavan Kumar K
  • 1,360
  • 9
  • 11
  • Okay ,Yes that's correct.I think ur code requires a lot of practice to reach there as i am only a beginner.Please Guide How to write code like yours it would be of great help.Also i haven't understood your else part – mansi kbchawla Jun 11 '15 at 06:11
  • .So,I assume that this was ur thought process in getting to the solution.Or if not then How did u get to the solution. – mansi kbchawla Jun 13 '15 at 02:12
0
int num = n;
for(i=9;i>1;i--)
{
   while(n%d==0)
   {
      n=n/d;
      arr[i++] = d;
   }
}

if(num<=9)
  arr[i++] = 1;

//printing array in reverse order;
for(j=i-1;j>=0;j--)
 system.out.println(arr[j]);
Ashish
  • 358
  • 2
  • 4
  • 15