This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont find any of the array is overloaded .... but i am getting arrayoutofbound exception ...
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic