Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing Strings.... without StringBuffer?

 
Andrew M.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...
So, I'm doing an assignment for my java class, and our teacher has written a halstead metrics program for us as a template.
We're to modify the program to apply to the java language, i.e. take a simple program and count all the different "verbs" and "nouns", then compute the halstead metrics for it.
The problem is recognizing symbols that do not have spaces surrounding them, i.e. args[], I need to count args, [, and ]. Now, is there anyway to write a file containing all the same information while putting spaces before and after such symbols... it's not implausable to write an if statement to find a [ and put spaces on either side.
But, because of how he's written the template using StringBuffer is going to be a massive amount of work and re-design of the template, which is to be avoided. And, replace() only works with characters... so... any ideas?
Thanks!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether I understood your task, but I guess you're mixing 'alphabetic characters' and 'char' from a programmers view.
If you get a line of code to parse, and firstly count the special characters, like ,;[](...;
In the next step you could replace every of them with a blank, wich is a char from a programmers view.
Then all your tokens should be seperated.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what a halstead metrics program is, but if you just want to put spaces around non-word characters [A-Za-z0-9], try this (needs java v1.4+)
 
Andrew M.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
Okay... this is pretty much what it's boiling down to now...
I have one large string... and within that string are, for an example, right and left parenthesis... I have to put spaces," ", before and after every one of them.
I'm not sure I quite grasp that above block of code, but, I'll give it a whirl.
 
Andrew M.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...
and, will replaceAll work with a normal string aswell as a stringbuffer?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replaceAll() is a method of the String class
to work with a StringBuffer you would need to call the toString() method first
if all you need is to find parentheses/braces/brackets ()/{}/[]
try this (delete any sets not required)
str = str.replaceAll("[\\[\\]\\{\\}\\(\\)]"," $0 ");
 
Andrew M.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that single line of code means wonders to me, thank you.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew M.,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
We'd like more than an initial for the last name.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic