Hi everyone,

I need to parse a text file based on a few delimiters. There are 2 problems I am having. I'll start with the first problem.

I have no problem using the split () method to parse text by a single delimiter. For example:

String[] str = stringRead.split(":");

However, I can't parse by : as it is sometimes used within the text. So, what if my decimeter was a string. For example:

String[] str = stringRead.split("Topic:");

How would I go about doing this? If I try it now, it parses by each letter in the text. Thank you for any help you can provide.

Just to add more detail to my first post. I could also parse the text using regex and the Pattern class:

http://java.sun.com/developer/technicalArticles/releases/1.4regex/

However, it only describes how to parse by a single character. I need to parse the text by an entire word.

So, for example, I would like to parse the following text by the keywords (Topic, Student, School):

Topic: the first topic is described Student: Joe Smith School: Central Topic: the second topic is described Student: Jill Smith School: West

I would like to get the text to look like this:

Topic: the first topic is described
Student: Joe Smith
School: Central
Topic: the second topic is described
Student: Jill Smith
School: West

I'm sure there is a better way to do this, but for lack of any other responses right now, why don't you use the substring method and the equals method to determine whether something matches the pattern?

Thank you. Would I be able to loop through the text using substring?

Wouldn't it stop after the first instance of the "Topic:" text?

No. You could find the correct indexes of what was before and after Topic: by searching for the substring Topic: within the String. Then the first half starts at index 0, and the index where Topic: starts is the end index. And the ending index of Topic: will tell you where the beginning index of the second part is.

Something else that came to mind:
Use the split method like this: String[] str = stringRead.split(" "); This will get you all the 'words' including the ":".
So loop throught the array until you find the ":".
You will know that, what is before the ":" and after should be at the same line.
When you find another ":". Print the previous token at a new line:

for (int i=0;i<str.length-1;i++) {
  if (str[i+1].equals(":")) {
      // the str[i] needs to go to a new line. change line
  }
   // print the str[i]
}

Make sure you make a good use of: System.out.println(); System.out.print("");

This article has been dead for over six months. Start a new discussion instead.