Forums Register Login

How to do find the braces?

+Pie Number of slices to send: Send
Hi... I am trying to find a curly brace out of a input file. The code is as following. But i get no output.My purpose is to find find the curly brace and load it inside an array and find the corresponding curly brace so that i can find cyclometic complexity within a method. please help me...
is there any other easier way of finding the position of method so that i can just get the codings within a method....


public String methodfromtokens()
{
String output = "", line = "";

try {
br = new BufferedReader (new FileReader(file));
while ((line = br.readLine()) != null)
{
StringTokenizer words =new StringTokenizer(line);
numtoken = words.countTokens();
String load[] = new String [numtoken];

int conubt = 0;
int j;

while (words.hasMoreTokens())
for( j=0; j<numtoken; j++)
{
load[j]= words.nextToken();

}


for ( j=0; j<numtoken; j++)
{

Pattern p = Pattern.compil("{");
Matcher m = p.matcher(load[j]);
found = m.find();

if (found == true)
{
System.out.println( "The braces found is " +m.group()+ "\n");
brace++;
}
System.out.println( "The number of braces is " +brace+ "\n");
}
}


System.out.println("Number of words : " + numtokens);
} catch (IOException e){}
return null;

}
+Pie Number of slices to send: Send
Two things to check into are:

- Curly braces are special characters in regexps, which need to be escaped. Try "\{" instead of "{". The javadocs of the Pattern class have more info on special characters.

- Instead of catching exceptions and returning null, at least print out a message that an exception occurred, so that you know if something went wrong.
[ May 23, 2006: Message edited by: Ulf Dittmer ]
+Pie Number of slices to send: Send
 

Originally posted by Ulf Dittmer:
Try "\{" instead of "{".



This is a good suggestion; note, though, that you'll have to write "\\{" (two backslashes.)
+Pie Number of slices to send: Send
thanks... i cannot do it with "\{"
But "\\{" -this works
thanks
Nothing? Or something? Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1272 times.
Similar Threads
error please help
method call
help with curly braces!!
help on output +=
help me to debug please
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:58:18.