Vipul Mishra
Vipul Mishra

Reputation: 1

string.indexof(")") not working

When i run following code its output is

lcp-   -1  ncp-   -1

I required the following output:

lcp-   8  ncp-   8

Code:

public static void main(String str[]) {
    String fun="M(a,b,c);"; 
    String inputset;
    char m,op;
    fun=fun.substring(0, fun.length()-2);
    //Vector<String> input=new Vector<String>();
    int indx=0;
    String cp;
    cp = ")";
    if(fun.charAt(0)=='M' &&  fun.charAt(1)=='('){
        int lcp=fun.lastIndexOf(cp);
        int ncp=fun.indexOf(cp);
        System.out.println("lcp-   "+lcp+"  ncp-   "+ncp);
    }
}

When I run the code it print -1, -1 for both lcp and nap. How can I fix it?

Upvotes: 0

Views: 650

Answers (2)

Tejaswini Chile
Tejaswini Chile

Reputation: 1

Still you can get answer : lcp- 7 ncp- 7

You need to add 1 before printing in b

System.out.println("lcp-   "+ (lcp+1) +"  ncp-   "+(ncp+1));

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

fun=fun.substring(0, fun.length()-2); will make fun equal to M(a,b,c. So, you will never have ) in the String. To remove ; from fun, use fun=fun.substring(0, fun.length()-1); instead.

Upvotes: 3

Related Questions