0

pred[] is a char array taken as input from the user. the expected input is in form predicate(param1,param2). this while loop is to detect the start and end indices of predicate, param1, and param2 but for some reason it is giving an exception.

    int k =0;
    while(pred[k]!="\0"){
            System.out.println("k="+k);
            if ("(".equals(pred[k])) {
                param1_st = k + 1;
                pred1_end = k - 1;
            }
            if (",".equals(pred[k])) {
                param1_end = k - 1;
                param2_st = k + 1;
            }
            if (")".equals(pred[k])) {
                param2_end = k - 1;

            }
            k++;

        }

output:

Enter a predicate 1
abd(sd,sdf)
Entered predicate is: abd(sd,sdf)
Enter a predicate 2
abd(x,db)
Entered predicate is: 
abd(x,db)k=0
k=1
k=2
k=3
k=4
k=5
k=6
k=7
k=8
k=9
k=10
Exception in thread "main" 
java.lang.ArrayIndexOutOfBoundsException:11
at unification.Unif.main(Unif.java:41)
Pshemo
  • 122,468
  • 25
  • 185
  • 269

1 Answers1

0

The condition in the while loop evaluates to a string comparison and in this case it will always return true value. A result is an infinite loop, since the k variable keeps incrementing, the index pointer will cause out-bound-exception when it goes beyond the array size limit.

while(pred[k]!="\0")

try to evaluate using length property instead.

while(k < pred.length )
owenrb
  • 535
  • 2
  • 7