Just wondering, what would the best way for pulling information from headings in a String be.
I have a class and getter methods for the headings (Summary, Heading 1, Heading 2).
For example,
If I have a String equal to,
This: is the first line of a String: xx-string: This is a long String
Summary:
This is the summary of the String
Heading 1:
This is the first heading
Heading 2:
This is another heading
What would be a desirable way to set the value of the Strings,
Summary, Heading 1, Heading 2
to
Summary = This is the summary of the String
Heading 1 = This is the first heading
Heading 2 = This is another heading
Thanks !!
Edit
Here is what I was going for,
public void setHeadings(Data fileData) {
String description = fileData.getDescription();
//String [] headings = description.split(":");
int indexOf;
indexOf = description.indexOf("Summary");
if(indexOf != -1)
{
String subString = description.substring(indexOf);
int indexOfNextHeading = subString.indexOf(":");
if(indexOfNextHeading != -1)
{
System.out.println(indexOf + ":" + indexOfNextHeading);
setSummary(description.substring(indexOf,indexOfNextHeading-1));
System.out.println(description.substring(indexOf,indexOfNextHeading));
}
}
}
That however, spits out an Array Out of bounds exception.
Use a Scanner object.
Then use it to read the String one line at a time.
Now sc.hasNext() tells you if there are lines left to read, and sc.next() returns the next line. Use this to iterate through the String one line at a time. You can test each line to see if it is equal to “Summary:” or “Heading 1:”, etc. Then you can use a StringBuffer to add each line from each String you want to create.
I can write this for you if you want, but it’s fairly simple.