RitvijSrivastava RitvijSrivastava - 22 days ago 5
Java Question

Why does my program keeps giving me an ArrayIndexOutOfBoundsException even tough I haven't got one

I was recently trying to build a program that takes two inputs and checks whether they are equally represented in other bases(bases are up till 20). But i keep getting the index out of bounds exception at line number 28...what to do?

For example: 12(base 10) = 5(base 3) [both are represented as '12' in their respective bases.]

import java.util.Scanner;
import java.util.Arrays;
class Bases
{

public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Two Numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Thank You for inputting the numbers!");

String basea[] = new String[20];
String baseb[] = new String[20];

int i=0 , j=0;
for( i=0;i<20;i++)
{
basea[i] = convert(a,i+1);
baseb[i] = convert(b,i+1);
}

for(i=0;i<=19;i++)
{
for(j=0;i<=19;j++)
{
if(basea[i].equals(baseb[j]))
{//this is where the exception keeps popping

break ;
}
}
}
if(i!=20){
if(i==0){
i=9;
System.out.println(a+"(base "+(i+1)+") ="+b+"(base "+(j+1)+")");
}
else
System.out.println(a+"(base "+(i+1)+") ="+b+"(base "+(j+1)+")");
}
else System.out.println("Numbers dont match at all till base 20!!");
}

private static String convert(int number,int base)
{

return Integer.toString(number,base);
}


}

Answer
for(j=0;i<=19;j++)

This above loop should be j <= 19

for(j=0;j<=19;j++)