Forums Register Login

Help needed in searching a particular string

+Pie Number of slices to send: Send
Dear all,

I am running a java program, where I am reading contents from a text file where the data retrieve will be as follows:



Now i want to search for the word |CARRIER| and if its found i need to aad that to an arraylist. Request you to help me out to achieve this without using the indexOf() method of the String class. How to achieve this using the delimiter.

Thanks in advance
+Pie Number of slices to send: Send
Personally, I would probably split that large string up, using the "|" as the split character

I would then loop through all of the string items and check equality against whatever criteria is needed

Then I would add it to an array and then do whatever else I needed to

There may be better ways, but would need some more info on what you need to do exactly in order to suggest better methods

Firstly, have a read of the java.lang.String documents : http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

I'd be interested in the split method
+Pie Number of slices to send: Send
Another option is to use a scanner.
+Pie Number of slices to send: Send
Here's another description of scanner, scanner example.
+Pie Number of slices to send: Send
I tried to make a sample code using scanner, but it doesn't work right, it only scans one character at a time. I've used this method before, but I don't see what's wrong.


Output one character at a time. This wasn't expected. Maybe I can't write code before 6 am.


+Pie Number of slices to send: Send
Scanner.useDelimiter takes a regular expression pattern. | has special meaning in regular expressions. You'll need to escape it; once with \ for the regex, and another \ to escape the \ in the Java string: useDelimiter("\\|").
+Pie Number of slices to send: Send
Thanks, Rob.
+Pie Number of slices to send: Send
You're welcome.
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1177 times.
Similar Threads
response.sendError()
Erro using HttpServletResponse.
Error Message.
Error Message.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:13:12.