I was working on a program that was outputting glossary terms and definitions in HTML and I want to vary the output based on whether the definition I'm checking has a keyword. To do this I have an ArrayList holding all the keywords I'm looking out for and I was trying to use a foreach loop but it wasn't really working right. Is there a better way to do this. Can someone offer me any pointers with this. I feel it shouldn't be THAT hard. With the HTML bit, I'm trying to output the leading words before the keyword (the keyword is supposed to be HREFED), and then continue past the keyword checking for any more keywords in the definition.
BY THE WAY: checkValues is my ArrayList and valueTwo is the definition.
This is what I have so far:
for(String getTerm : checkValues){
String correct = getTerm + " ";
if (valueTwo.contains(correct)) {
int foundTermPosition = valueTwo.indexOf(correct);
lead = valueTwo.substring(0, foundTermPosition-1);
index = valueTwo.length() - lead.length();
leftOver = valueTwo.substring(foundTermPosition+correct.length(), valueTwo.length());
out.write(lead);
out.write("<A HREF=\"#" + correct + "\">" + correct + "</A>");
out.write(leftOver + "\n");
}
else
{
out.write(valueTwo);
}
}
EDIT: After your explanation what you have to do is this:
String valueTwoWords [] = valueTwo.split(" "); // if value = "I went"
// valueTwoWords = ["I","went"]
for(String getTerm : checkValues)
{
for(int i = 0; i < valueTwoWords.length(); i++)
{
if(valueTwoWords[i].compareTo(getTerm) == 0) // If the words are the same.
{
int foundTermPosition = valueTwo.indexOf(getTerm);
lead = valueTwo.substring(0, foundTermPosition-1);
index = valueTwo.length() - lead.length();
leftOver = valueTwo.substring(foundTermPosition+getTerm.length(), valueTwo.length());
out.write(lead);
out.write("<A HREF=\"#" + getTerm + "\">" + getTerm + "</A>");
out.write(leftOver + "\n");
i = valueTwoWords.length() + 1; // getting out of the cycle.
}
}
}
You should not add a black space to the string:
String correct = getTerm + " ";
try to compare directly over the string:
if (valueTwo.contains(getTerm))
If you have the following Strings:
String s1 = "hi";
String s2 = "hi";
String s3 = "hi ";
this s1.contains(s2));
will return true
but s1.contains(s3));
will return false
.
If you objective is to see if two word are the same you should use the method compareTo
instead of the method contains.
compareTo
public int compareTo(Object o)
Compares this String to another Object. If the Object is a String, this function behaves like compareTo(String). Otherwise, it throws a ClassCastException (as Strings are comparable only to other Strings).
note that:
Returns:
the value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. (source)
So for string1
and string2
string1.compareTo(string2)
will return 0 if there are equal. If this what you want than do:
if (valueTwo.compareTo(getTerm) == 0)
Finally if you object is just check if valueTwo is on the ArrayList once, you can use
if(checkValues.contains(valueTwo))
// do something