Reputation: 503
I am working on a problem where I have List of Strings which is nothing but a java code. So if that code has any declarations before or after method body, then this script should give me an error. If the variable declarations are inside the method body then it should work fine.
for ex:
int abcs; // This should generate error.
double cad; //This should generate error.
Void main(){
int test; //This should not generate error.
void test1(){
int test3; //This should not generate error.
}
}
I have wrote a small program for this but could not able to get the desired results, My code is :
private void check( String line)
{ int count = 0; int flag = 0;
// is there an open brace on the current line
int bracketpos = line.indexOf('{');
// is there a close brace on the current line
int closebracketpos = line.indexOf('}');
// is there a semicolon on the current line
int charpos = line.indexOf(';');
// increment count if there is an open brace
if(bracketpos>=0 )
++count;
// decrement count if there is a close brace
if(closebracketpos>=0 )
--count;
// if there is an open brace on the current line
// ...and there's a close brace on the current line
// ...and there's a semicolon on the current line
// ...and the number of brackets are balanced (count==0)
if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){
// if the first semicolon appears after the first close brace
if(charpos>closebracketpos){
flag =2;
}
else
// the close brace appears after the semicolon, or neither exist
flag =0;
}
else if (charpos >= 0 && count ==0) {
flag= 1;
}
else {
flag = 0;
// the line does not have a ;,{,}
}
if(flag!=0)
{
//ERROR
}
}
I am passing line by line of my example, so count is keeping record of "{" and "}".
PROBLEM: I am feeding my example to the check() line by line. And I would like to check whether are there any declarations in the example. But If there are any declarations inside the method body then it is fine, but if there are declarations out side the method body then my check() should prompt/ or give me an error.
Upvotes: 0
Views: 86
Reputation: 503
I experimented and finally got it working, The only problem was count was initialized to 0 every time check() is called.
So I came up with two alternatives, either make count static global int. Or pass the count value with check() and updated count should be returned so as to keep the record of the "{" and "}".
Upvotes: 0
Reputation: 10326
Your logic is wrong. You should add comments describing what the code does at each stage. Here's to get you started...
private void check( String line)
{ int count = 0;
int flag = 0;
// is there an open brace on the current line
int bracketpos = line.indexOf('{');
// is there a close brace on the current line
int closebracketpos = line.indexOf('}');
// is there a semicolon on the current line
int charpos = line.indexOf(';');
// increment count if there is an open brace
if(bracketpos>=0 )
++count;
// decrement count if there is a close brace
if(closebracketpos>=0 )
--count;
// if there is an open brace on the current line
// ...and there's a close brace on the current line
// ...and there's a semicolon on the current line
// ...and the number of brackets are balanced (count==0)
if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){
// if the first semicolon appears after the first close brace
if(charpos>closebracketpos){
flag =2;
}
else
// the close brace appears after the semicolon, or neither exist
flag =0;
}
else if (charpos >= 0 && count ==0) {
flag= 1;
}
else {
flag = 0;
// the line does not have a ;,{,}
}
if(flag!=0)
{
//ERROR
}
}
Upvotes: 1