aspose file tools*
The moose likes I/O and Streams and the fly likes Getting position of a word in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Getting position of a word in a text file" Watch "Getting position of a word in a text file" New topic
Author

Getting position of a word in a text file

Axl Rose
Greenhorn

Joined: Oct 06, 2002
Posts: 6
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

Joined: Sep 28, 2002
Posts: 86
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;
}


We make a living by what we get, we make a life by what we give!
Axl Rose
Greenhorn

Joined: Oct 06, 2002
Posts: 6
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

Joined: Oct 06, 2002
Posts: 6
THE ABOVE WORD "MAROON" SHOULD BE INDENTED. WHEN IT POSTED, IT JUSTIFIED IT.
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
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

Joined: Oct 06, 2002
Posts: 6
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

Joined: Sep 28, 2002
Posts: 86
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

Joined: Oct 06, 2002
Posts: 6
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Oct 06, 2002
Posts: 6
Sorry about that
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
How's this?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"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

Joined: Oct 11, 2002
Posts: 6
Darn, I thought it was THE Axel Rose looking for a new career in computer programming!
Glenn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting position of a word in a text file