aspose file tools*
The moose likes Java in General and the fly likes help me to point out the error in the program .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help me to point out the error in the program .... " Watch "help me to point out the error in the program .... " New topic
Author

help me to point out the error in the program ....

Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
the concept of the program is ... if ypu enter the input as 'ABDF' , the output should be 110101 ... for A.B,D,F which is present 1 is replaced .. for the missing c and e , 0 is added ...

the length is assigned as 6 .. so if we enter 'A' , then the output should be 100000 ... but i am getting an arrayoutofbound exception ... can you help me to find the error...

here is the program ..............

class TCheck
{
public static void main(String [] args)
{
int blen = 6;
String aString = "ABC";
int binArray[] = new int[blen];
int c,k=0,asciiVal=65,x=0;
char chArray[] = aString.toCharArray();
for(int i=0;i<chArray.length;i++)
System.out.println(chArray[i]);
for(int i =0;i<blen;i++)
{
if(chArray[x] == asciiVal)
{
binArray[k] = 1;
x++;
}
else
binArray[k] = 0;

k++;
asciiVal++;

}
for(int i=0;i<k;i++)
System.out.println(binArray[i]);
int l =0,tenPower,binaryValue=0,n=k-1;
for(int i=0;i<k;i++)
{
tenPower = (int)Math.pow(10,n);
binaryValue = binaryValue +( binArray[i] * tenPower);
n--;
}
System.out.println("binaryValue --------- "+binaryValue);
}
}


Manisekar
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
i dont find any of the array is overloaded .... but i am getting arrayoutofbound exception ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

The error is here.....



The chArray array which is holding your string, is only three is size. Yet, you are treating it a blen (six) is size.


EDIT --> Okay it is slightly more complicated. Basically, you ran out of the source string (at 3), but you are still checking the rest of blen, which overflows since there is no more source characters left.

Henry
[ December 02, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
yup i know the exception is thrown at that point ... how can i solve that ... but when i use 'AF' as input i am getting the result ... in this case, the arraylength is 2 only ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Originally posted by Manisekar Chinnasami:
yup i know the exception is thrown at that point ... how can i solve that ... but when i use 'AF' as input i am getting the result ... in this case, the arraylength is 2 only ...


Currently, your "for" loop checks all the ascii values in blen. This is what is causing the problem. You need to exit the "for" loop if either you run out of blen ascii characters, or characters in your source string. You are missing the second exit condition, which causes you to loop for more source characters, hence, the out of bounds exception.

You can probably solve this by adding an "if" check to see if you are out of characters, and "break" out of the loop, if you are.


BTW, the reason "AF" is working is because the blen "for" loop, and the source ends at the same time. Try "ABCDE", you will see that it won't work -- because the check for "F" will be at a point when you are out of characters.

Henry
[ December 03, 2007: Message edited by: Henry Wong ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help me to point out the error in the program ....