• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disply the NUmber of Characters between a given sign

 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Winston Gutkowski
Bartender
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the output say for the example, if the String is:

The output should be:




oe, Including the spaces

 
Winston Gutkowski
Bartender
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic