开发者

how to solve this exception :Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

开发者 https://www.devze.com 2023-01-30 09:17 出处:网络
i got this exception after i run my code i don\'t know what 开发者_运维问答is the problem!! the program initially creates int array then string array with the same length same as int,then take every

i got this exception after i run my code i don't know what 开发者_运维问答is the problem!!

the program initially creates int array then string array with the same length same as int,then take every index in v and convert it to binary and adds zeroes if the digits less than 4 then store it in the string index after manipulating the string array it passes it to method Called toBinaryInteger which return int array the toBinaryInteger method creates an array of length st.length*4 which supposed to be 32 then takes each entery from st and takes each digit of it and stores it in the p array then the program print the result but the exception is shown up at this moment of time . i hope i explain the program very well . any idea help please.

hi every body problem solved the problem is i was printing the wrong array insted of result i printed st special tankx for the good debugger

 public static void main(String [] arg) 
    {   
        int [] v={0,11,12,13,14,15,7,8};
        String [] st=new String [v.length];
        String x="";
        for(int i=0;i<st.length;i++)
        {
            x=Integer.toBinaryString(v[i]);
            while (x.length()<4)    // add zeroed to left if needed to fit in 4 bits
                x="0"+x;
            st[i]=x;


        }
        int [] result=toBinaryInteger(st);
        int count=0;
        for(int k=0;k<result.length;k++)
        {
            System.out.print(st[k]);
            if(count==4){
                System.out.print("  ");
                count=0;
            }

        }



    }   






public static int [] toBinaryInteger(String [] s)
    { 
        int [] p=new int [s.length*4];
        for(int i=0;i<s.length;i++)
            {
            for(int j=0; j<s[i].length();j++){
                p[i*4+j]=Integer.parseInt(s[i].substring(j,j+1));//create array of 32 lenght    

                }
            }
            return p;   
    }


I compiled your code. The resulting error told me that the problem appears to be this line:

System.out.print(st[k]);

Perhaps you meant to print result[k] there instead?

0

精彩评论

暂无评论...
验证码 换一张
取 消