#Java Code Error

1 messages · Page 1 of 1 (latest)

merry mantleBOT
#

<@&987246399047479336> please have a look, thanks.

deft granite
#

Your error message is truncated. Please post it as plain text.

buoyant cosmos
#

Your main method expects to receive two arguments. If it doesn't it exits with the result 1 (non-zero results are considered errors).

#
        if (args.length != 2) {
            System.err.println("Usage: java SearchByLyricsWords <songFile> <lyricsWords>");
            System.exit(1);
        }
#

You could select the text, copy to the clipboard and then paste here like this

```
Some text...
```

would look like

Some text...
#

However in this case it wouldn't help... the error is not in the code, it's in the options being passed to the program when launching it.

#

` is a 'back-tick' character, not the . (period) or a ' (apostrophe). On many keyboards it's located to the left of the 1 key above the ~

#

There's no line to change. The program wants two arguments, a songfile, and lyrics words.

#

How are you running it?

#

Which IDE is it? NetBeans?

#

The arguments must be passed to it by the thing that runs it. Since it's not prompting the user for them

#

The 'Arguments' field only has a single arg, it's needs two - the second should be the lyrics words.

#

eg allSongs.txt these,are,some,words

#

Or, if you want to use spaces you might be able to 'quote' them... allSongs.txt "these are some words"

#

If you were runing the application in a shell you'd say...

$ java student.SearchByLyricsWords allSongs.txt these,are,some,words
#

That comma between the args needs to be removed.

#

Args are 'space-delimited'

#

Now you have a real bug. You're trying to store String results in an array of Song.

#
          Set<String> results = null;
          ...
          return results.toArray(new Song[0]);
#

Results is a Set containing String references. new Song[0] tells the toArray method to store these in an array of type Song[] and Strings are not Songs.

worn raven
#

Do you have an English keyboard? ` is before 1 on the keyboard, not the same as a .

#

Thats it!!!

#

dont worry

buoyant cosmos
#

Firstly... search has no access to the songs originally supplied to the constructor, so it can't return songs.

Secondly it's not entirely clear on the intention of search. It looks like you're trying to find the set of songs that contain all of the words in the lyrics passed to the method.

However the only state that the class has, is a mapping of lyric-words to song-titles.

#

The filtering of non-words, short-words and common-words is fairly straightforward. But it's not clear what you mean by 'phrase' - you don't have any record of word adjacency in the song-lyrics to compare a phrase to.

#

I can suggest improvements to what the code is doing but I can't really comment on whether it's doing the right thing without properly understanding the requirements.

#

eg This...

private static final String common_Words = "the of and a to in is you that it he for was on\n" +
" are as with his they at be this from I have or\n" +
" by one had not but what all were when we there\n" +
" can an your which their if do will each how them\n" +
" then she many some so these would into has more\n" +
" her two him see could no make than been its now\n" +
" my made did get our me too";

Should probably be...

private static final String COMMON_WORDS = """
    the of and a to in is you that it he for was on
    are as with his they at be this from I have or
    by one had not but what all were when we there
    can an your which their if do will each how them
    then she many some so these would into has more
    her two him see could no make than been its now
    my made did get our me too""";

And I'd probably split it into a Set<String> right there, and provide a private method for boolean isCommonWord(String word) to use as a filter.

#

So the assertion in "What to Expect" is presumptive. You don't have a map of words to sets of songs. You have a map of words to sets of song-titles.

#

Your state is Map<String, Set<String>> not Map<String, Set<Song>>

#

There can be many songs of the same title with different lyrics (and artists)

#

Search is supposed to return the songs, but you don't have them, you only have the titles.

#

Assuming you did have songs, you have two options.

  1. Start with a 'matches' set of all songs, for each lyric-word, intersect 'matches' with the songs having that lyric word.
  2. Start with an empty set of songs, for the first lyric-word, add to 'matches' the songs having that lyric word, then for each other lyric-word, intersect 'matches' with the songs having that lyric word.

You null-handling is more or less this.

However... It's not clear what role TreeSet would have, unless it's to sort the songs - though most likely the better performance option for any measurably large number of intersections and song lists would be to use HashSet and then just sort the Song array at the end.