This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do find the braces?  RSS feed

 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

}
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Try "\{" instead of "{".


This is a good suggestion; note, though, that you'll have to write "\\{" (two backslashes.)
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks... i cannot do it with "\{"
But "\\{" -this works
thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!