aspose file tools*
The moose likes Beginning Java and the fly likes Disply the NUmber of Characters between a given sign Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Disply the NUmber of Characters between a given sign" Watch "Disply the NUmber of Characters between a given sign" New topic
Author

Disply the NUmber of Characters between a given sign

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

HI Guys I have a File that is in this format:




I need to read just the first line an determine how many characters are between the sign "|". I need the code to read even the spaces.

This is the code I have:



Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Stanley Mungai wrote:I need to read just the first line an determine how many characters are between the sign "|". I need the code to read even the spaces.

Well your code seems to be doing an awful lot more than that. Surely the number of characters between the "|"s is just the length of each word?

However, a few tips for you:
1. is redundant, since you're basically doing the same check twice.

2. Since String.length() returns a constant, you can speed up the loop a bit with:
   for (int k = 0, e = words[i].length(); k < e; k++) {
in this case the saving is miniscule, but the technique is worth knowing.

3. Kind of similar. If you find that you need to do more than one check for the character contents, store it first, viz:
HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

I need the output say for the example, if the String is:

The output should be:




oe, Including the spaces

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Stanley Mungai wrote:I need the output say for the example, if the String is:...
oe, Including the spaces

And have you tried printing out the lengths of your "words", as I suggested?

Winston
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Well This is the new Code that I am having. I have followed your advice to try and Reduce the Intensity Of the code: Where could the Error be?

William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

I think your problem is with the automatic trimming of spaces.

If so, you probably cannot use the split() function, but write your own splitter, count the characters and save the results.

WP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

William P O'Sullivan wrote:I think your problem is with the automatic trimming of spaces.

I don't think so. There's nothing in the docs about trimming, unless its something new for v7.

Stanley Mungai wrote:Well This is the new Code that I am having. I have followed your advice to try and Reduce the Intensity Of the code: Where could the Error be?

Well, apart from the fact that what you've supplied won't compile, I have no idea.

Please TellTheDetails (←click). What error are you getting?

Winston

PS: Another tip for you. split() returns an array, for which you can use a foreach loop, so you could rewrite your code as:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disply the NUmber of Characters between a given sign