0

I wrote a piece of code to look through a String named line with several lines(hence newline characters) in it. my code prints all locations of the string Context in the line. how do i modify the code to print the occurence

String Context = ("[020t");
int index =0;
int count = 0;
while((index = line.indexOf(Context, index))!= -1)
{
    count++;
    index += Context.length() -1;
    System.out.println(index);
}
System.out.println(count);

A sample line is

[020t 12:23:43 FILE TAKEN
[020t 12:23:44 REGISTRATION END
[0r(1)2[000p[040qe1w3h162[020t*881*11/11/2010*12:24*
     *CARD INSERTED*
[020t 12:24:06 CODE ENTERED
    11\11\10     12:24     10390011
5061180101607659013   6598
  INVALID TRANSACTION, PLEASE CONTACT
YOUR ADMINISTRATOR FOR ADVICE

I intend to pass the lines out to another method. Thanks

Zigmaphi
  • 15
  • 5

1 Answers1

0

If you intend to pass the occurance, split the string based on new line. iterate the resultant string array and check if it starts with your needed string - if so you can add it some list and pass the list to your method

Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29
  • splitting based on a new line is giving me errors. 'line = (resultset.getString("activity")); System.out.println(line); String lines[] = line.split("[\r\n]+");' the output for lines is **[Ljava.lang.String;@197a37c** while for line is **Sample String** – Zigmaphi Sep 10 '13 at 13:59
  • try String.split("\\r?\\n"); from http://stackoverflow.com/questions/454908/split-java-string-by-new-line – Balaji Krishnan Sep 10 '13 at 14:11
  • I looked at that post severally, and didn't get a suitable solution. i'm coding for a java 1.4 application(i don't think it matters though). – Zigmaphi Sep 10 '13 at 14:21