Why doesn't a string that explicitly contains part of another result in a hit hit using .contains ()?

this program tries to scan a music magazine in a text file and then match songs to the wav files directory. all files are named with the same convention: artist-title, that is: lukebryan-kickthedustup.wav. I swap titles and artist using a separator function that makes it easy to compare to a music magazine that's already formatted in the same way: title, artist.

now let's say I'm looking for the term "lovingyoueasyza" which is Loving You Easy by Zac Brown Band ... when it reaches a file in a directory with the line "lovingyoueasyzacbrownband" assigned, it ignores it even if it contains that line. you will see that I am calling:

if(searchMe.contains(findMe))

      

but it doesn't return the hit. it will return matches if the findMe string only contains the song title, but if any part of the artist name crawls into that string, it stops working. What for!? for shorter titles its critical i can also search for artist name so i can't just search by song title.

I've tried using .trim () to no avail. here are some examples of the output when a match is found:

searching term: "onehellofanamen"
comparing to: "onehellofanamendbrantleygilbert"
Match found!
value of findMe: onehellofanamen
value of searchMe: onehellofanamendbrantleygilbert
value of y: 49
value of x: 79

      

Here's an example of the output of a failed attempt:

searching term: "lovingyoueasyza"
comparing to: "keepmeinminddzacbrownband"
searching term: "lovingyoueasyza"
comparing to: "lovingyoueasydzacbrownband"
searching term: "lovingyoueasyza"
comparing to: "nohurrydzacbrownband"
searching term: "lovingyoueasyza"
comparing to: "toesdzacbrownband"
searching term: "lovingyoueasyza"

      

this is what findMe comes in the method as:

fileToProcess var is: C:\test\06012015.TXT
slot #0: topofhouridplac
slot #1: lovemelikeyoume
slot #2: wearetonightbil
slot #3: turnitoneliyoun
slot #4: lonelytonightbl
slot #5: stopset
slot #6: alrightdariusru
slot #7: lovingyoueasyza
slot #8: sundazefloridag
slot #9: stopset

      

the final output of matchFound looks like this:

Item Number: 0 ****TOP OF HOUR****
Item Number: 1 d:\tocn\kelseaballerini-lovemelikeyoumeanit.wav
Item Number: 2 null
Item Number: 3 null
Item Number: 4 null
Item Number: 5 ****STOP SET****
Item Number: 6 null

      

... in 82.

public static String[] regionMatches(String[] directoryArray,
            String[] musicLogArray) throws InterruptedException {

        String[] matchesFound = new String[musicLogArray.length];
        String[] originalFileList = new String[directoryArray.length];

        for (int y = 0; y < directoryArray.length; y++) {

            originalFileList[y] = directoryArray[y];

            System.out.println("o value: " + originalFileList[y]);
            System.out.println("d value: " + directoryArray[y]);

        }

        for (int q = 0; q < originalFileList.length; q++) {
            originalFileList[q] = originalFileList[q].replaceAll(".wav", "");
            originalFileList[q] = originalFileList[q].replaceAll("\\\\", "");
            originalFileList[q] = originalFileList[q].replaceAll("[+.^:,]", "");
            originalFileList[q] = originalFileList[q].replaceAll("ctestmusic",
                    "");
            originalFileList[q] = originalFileList[q].replaceAll("tocn", "");

            originalFileList[q] = originalFileList[q].toLowerCase();
            String[] parts = originalFileList[q].split("-");
            originalFileList[q] = parts[1] + parts[0];
            System.out.println(originalFileList[q]);
        }

        for (int x = 0; x < musicLogArray.length; x++) {

            for (int y = 0; y < directoryArray.length; y++) {

                //System.out.println("value of x: " + x);
                //System.out.println("value of y: " + y);
                String searchMe = originalFileList[y];
                String findMe = musicLogArray[x];
                int searchMeLength = searchMe.length();
                int findMeLength = findMe.length();
                boolean foundIt = false;
                updateDisplay("searching term: " + "\"" + findMe+"\"");
                updateDisplay("comparing to: " + "\"" + searchMe + "\"");

                //for (int i = 0; i <= (searchMeLength - findMeLength); i++) {

                    if(searchMe.contains(findMe)){
                        updateDisplay("Match found!");
                        updateDisplay("value of findMe: " + findMe);
                        updateDisplay("value of searchMe: " + searchMe);
                        updateDisplay("value of y: " + y);
                        updateDisplay("value of x: " + x);

                        matchesFound[x] = directoryArray[y];
                        break;

//                  if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {
//                      foundIt = true;
//                      updateDisplay("MATCH FOUND!: "
//                              + searchMe.substring(i, i + findMeLength));
//
//                      matchesFound[x] = directoryArray[y];
//
//                      break;
                    } else if (findMe.contains("stopset")){

                        matchesFound[x] = "****STOP SET****";
                        break;

                    } else if (findMe.contains("topofho")) {

                        matchesFound[x] = "****TOP OF HOUR****";
                        break;

                    }
                }
                //if (!foundIt) {
                //  updateDisplay("No match found.");

                //}

            }

        //}
        return matchesFound;
    }

      

+3


source to share


2 answers


It looks to me like your music directory has a bunch of unwanted d's in the file where you put the parts back.

search term: "lovingyoueasyza" versus: "lovingyoueasydzacbrownband"



The string comparison does not contain the search term, because after the "simple" there is a "d" that breaks the search, so you have errors including artist names.

+6


source


Here:

searching term: "lovingyoueasyza"
comparing to: "lovingyoueasydzacbrownband"

      

On your second line, notice that there is extrad

after light use .
Therefore the second line does not contain the first line.



I think you are adding an extra 'd' when you concatenate the song name with the artist name.
The same happens for all your other strings, eg.

searching term: "onehellofanamen"
comparing to: "onehellofanamendbrantleygilbert"

      

which I assume is one hell of an amen

+ extra 'd' + brantley gilbert

.

+4


source







All Articles