Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding a char sequence in a file  RSS feed

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I want to find out the occurrence of a particular character sequence in a file. For example, I have in a txt file the following firstname=jot&#number;&#number|lastname=kum&#number;&#number|. Now I want to read this and whenever I encounter &#number;, I want to send that particular String and pass it to a function that will do some decoding and return me a String and once I get that String returned, I append it to the contents that have been read so far from the file and can proceed reading the file further and repeat the same process where ever I encounter such &#number; format. Finally I want to print the entire thing to another file. Any suggestions?

[ December 12, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]

[ December 12, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ December 12, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I achieved this today. I got some input from one of the posts here. Here is what I did,



I'm actually reading a file that has the following text,

firstname=jot&#number1;&#number2;|lastname=shank&#number3;&#number4;ku&#number5;ar|

NOTE: Actually in the places of number1, number2, number3, number4, number5 replace them with 100, 200, 300, 400 and 500 respectively because when I submit this here, it gets converted into some unknown format.

So after I run my above program, I get the output as,

firstname=jothi|lastname=shankarkumar| which is what I wanted.
[ December 12, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are trying to achieve can be easily done using regular expressions.

Following code will do it for you (It uses the decodeMethod() that you have provided.):



However, there is a border case where in this will not work, if the whole of the contents of the file are not fed to this method.
The case being that the part of the file you read does not have the complete pattern in it i.e. if the file contains:

firstname=jot&#number1;&#number2;|lastname=shank&#number3;&#number4;ku&#number5;ar|

It may so happen, that for an execution you only read:

firstname=jot&#number

and thus the pattern is not completed and hence will not be replaced.

So, it all depends in the way you read the file but the above will work for a valid input string.

P.S. : There is a better way of wrtiting the decode method. Since, you are only determining the return value based on the number. You can extract the number from the pattern and do either a switch case OR you can create an array that has the to be returned characters/string stored at the index that they are suppose to replace.
Hope you get this.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Thanks for the post. But the program that I'm writing is for older jdk version and I'm not sure if regex works with that.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Hey,

Thanks for the post. But the program that I'm writing is for older jdk version and I'm not sure if regex works with that.


Its been there since jdk 1.4. Are you on the version before that?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess not. Anyways thanks for the information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!