Find two equal substrings in a string
I'm not completely sure about the requirements, but what about the following?

public static String findWord2(final String text) {
    for (int i = text.length() - 1; i &gt; 0 ; i--) {
        final String first = text.substring(0, i);
        final String last = text.substring(text.length() - i);
        if (first.equals(last)) {
            return first;
        }
    }
    return text;
}


Here are my tests:

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FindWords2Test {

    private final String expected;
    private final String input;

    public FindWords2Test(final String expected, final String input) {
        this.expected = expected;
        this.input = input;
    }

    @Parameters
    public static Collection&lt;String[]&gt; data() {
        //@formatter:off
        final String[][] data = new String[][] { 
                {"file", "file = new JarFile(new File()\nSystem.out.println(file"},
                {"file ", "file = new JarFile(new File()\nSystem.out.println(file "},
                {"file = new JarFile(new File()\nSystem.out.println(", 
                    "file = new JarFile(new File()\nSystem.out.println("}, 
                {"args", "args"},
                {"", ""}, 
                {"a", "a"}, 
                {"a", "aa"}};
        //@formatter:on
        return Arrays.asList(data);
    }

    @Test
    public void testFindWords() throws Exception {
        assertEquals(expected, FindWords2.findWord2(input));
    }
}