• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting position of a word in a text file

 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to get a position of a word in a file (i.e. it's height and width)?
I read in a file. In the file, I have words that are indented and some that aren't. Indented words are CHILDREN of the above word. Is there a method that tracks page location, so I can see if a word is indented or not?
Thanks, Matt
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This a function that I wrote to find and remove a portion of the string! You should be able to modify this to make it work. In the main part of the program you could read line by line from the file and send the line in to this function! I hope this helps, Good luck!
public static String RemoveSubstring(String s, String rs) {
// removes the first accurance of rs in a string
// and sends back the new string!
int len = s.length(), pos, rslen = rs.length();
String firsthalf, lasthalf, result;
pos = s.indexOf(rs, 0);
firsthalf = s.substring(0, pos);
lasthalf = s.substring(pos + rslen, len);
result = firsthalf + lasthalf;
return result;
}
 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Unfortunately, this won't help my problem.
I need to find the x,y, coordinate is a word in a file.
For example if this was a text file:
Red
Maroon
I could see that the word "red" has an x-coordinate of say 10, and "maroon has an x-coordinate of 20. Thus I would know that marron is indented under red.
Thanks again for the reply.
 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THE ABOVE WORD "MAROON" SHOULD BE INDENTED. WHEN IT POSTED, IT JUSTIFIED IT.
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it help if you did an if statement for a tab, '\t'? You would still have to parse each line no matter what you do, and alot is going to depend how the text file was created; did they press the space bar, tab key?
 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah it seems like the space bar was used to indent, b/c the indentations aren't that deep. i am not sure what to do!
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand what you are trying to do, so why not start at the beginning of the line and test for spaces until you reach the first letter?
String source; //line coming in
char spac=' ';
int i, pos, sp = 0, len = source.length();
for (i = 0; i < len; i++) {
if (source.charAt(i)==' ') {
sp = sp + 1;
}
}
 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'll give it a shot, for some reason i was thinking of doing this before i read in the file. thanks.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,
Could you please change your display name to comply with JavaRanch's naming policy? (It's our only rule.) You can make the change quickly right here. Thanks, and welcome to JavaRanch!
Pauline
 
Axl Rose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, "Axl Rose" seems to qualify as "obviously fictitious", which is also against our policy. Why not something like, say, "Matt Scotch"? Or if you want to use something made-up, try to be less obvious.
The same goes for "Yosemite Sam" of course. :roll:
[ October 10, 2002: Message edited by: Jim Yingst ]
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How's this?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Yo Sam" ? Ummm, borderline. It might slip under the radar normally (with us thinking maybe it's an Asian name of some sort) but under the circumstances, we'd really like something that looks more like a real person's name. Thanks.
 
Glenn Grieb
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn, I thought it was THE Axel Rose looking for a new career in computer programming!
Glenn
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic