Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450
Accepted Solution

split a file and search the individual strings?

[ Edited ]

hi friends...

am writing a code which reads a text file and thier is option to search by name

if we give name then it has to search the file and if it matches it has to print the entire line..

 

but am able to split the text file but i have the problem with printong the specified line

 

so any one please help me

 

my code is..

 

 

fconn = (FileConnection) Connector.open("file:///store/home/user/newfile.txt", Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
Vector nodes = new Vector();
String separator = ",";
System.out.println("split start...................");
// Parse nodes into vector
int index = result.indexOf(separator);
while(index>=0) {
nodes.addElement( result.substring(0, index) );
result = result.substring(index+separator.length());
index = result.indexOf(separator);
}
// Get the last node
nodes.addElement( result );

// Create splitted string array
String[] res = new String[ nodes.size() ];
if( nodes.size()>0 ) {

res[1] = (String)nodes.elementAt(1);
String nam = res[1].toString();
String name = "akhil";
if(nam.equals(name)==true){
res[7] = (String)nodes.elementAt(7);
String st = res[7].toString();
add(new BasicEditField("phonenumber: ",st));

 

 

and my file contains:

akhil,9999999999,hyderabad

anil,9999333333,hyderabad

gabby,9898989898,secunderabad

 

Please use plain text.
Developer
peter_strange
Posts: 19,513
Registered: ‎07-14-2008

Re: split a file and search the individual strings?

I have similar code to this and I do the processing in two steps.

1) First I go through the bytes splitting it at new line and/or carriage return ("\n" & "\r").  I throw away empty lines - this means it works fro text files from Unix, which only have line feed and from Windows which tend to linefeed and carriage return. 

2) Then i process each line and break this up on the ",".  That way I have the full line and the associated individual entries. 

 

I think a similar approach will work for you too. 

 

My code is too specific to be useful to you, hopefully the idea will work.

Please use plain text.
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450

Re: split a file and search the individual strings?

Can i get your code so that it may be more easy for me ....

Please use plain text.
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450

Re: split a file and search the individual strings?

yeahh i have seperated by each line as you said but i have some complexity in breaking each line 

so can i get your code are any help fom you

Please use plain text.
Developer
peter_strange
Posts: 19,513
Registered: ‎07-14-2008

Re: split a file and search the individual strings?

This is not my code, not actually sure where I got it from.  To use it you extract each line, then convert the line bytes into a String, then use this code to break the line up into the 'Strings'.  Token wil be the ','. 

       

// This has not been tested properly and I doubt very much that it will cope

       

// with end cases like a string containing just the token.

       

public static String [] split(final String s, finalchar token) {

               

int currentIndex = 0;

               

if ( s == null ) {

                       

returnnull;

                }

               

int index = s.indexOf(token);

                Vector splitVector =

new Vector();

               

while ( index > 0 ) {

                        splitVector.addElement(s.substring(currentIndex, index));

                        currentIndex = index+1;

                        index = s.indexOf(token, currentIndex);

                }

                splitVector.addElement(s.substring(currentIndex));

                String [] returnStringArray =

new String [splitVector.size()];

               

for ( int i = 0; i < returnStringArray.length; i++ ) {

                        returnStringArray[i] = (String) splitVector.elementAt(i);

                }

               

return returnStringArray;

        }

Please use plain text.
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450

Re: split a file and search the individual strings?

Well yeahh i did the same thing has you suggested but am getting Stringindexoutofbound exception.

 

Please use plain text.
Developer
peter_strange
Posts: 19,513
Registered: ‎07-14-2008

Re: split a file and search the individual strings?

Sounds like a good time to roll out the debugger....

Please use plain text.
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450

Re: split a file and search the individual strings?

Thank you peter it really helped me alot..

Now am able to  break each line and print it

 

Thanks a lot for your help for making me know about breaking the Stringa

 

thanks once again...

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: split a file and search the individual strings?

a thread with 3 solutions, new record i guess :smileyhappy:
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
akhilmuppa
Posts: 41
Registered: ‎11-18-2012
My Carrier: 9951111450

About personalization?

[ Edited ]

I want to add personalization feature which have to replace the name with the original name from text file.

 

for example:

      it we type message like

                               you %name% from %city%

 

 

if we type such message it has to replace the name with the original name checking from text file

 

 

can you help me to add this feature..

Please use plain text.