• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help needed in searching a particular string  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to use a scanner.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another description of scanner, scanner example.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("\\|").
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob.
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!