-3

I am trying to create a method that returns an array that contains String representation of all MediaItem whose author matches the targetAuthor passed in. The array cannot contain any null values. If there are no matches the method returns an array of length 0. Here is my code

private ArrayList<MediaItem> itemList;

public MediaList(){
          itemList = new ArrayList<MediaItem>();
       }

...

public String[] getAllItemsByAuthor(String targetAuthor){
      Object tempArray[] = itemList.toArray();
      String targetAuthorArray[] = new String[tempArray.length];
      for (int i = 0; i < tempArray.length; i++){
         targetAuthorArray[i] = (String)tempArray[i];
      }
      int index = 0;
      for (String value : targetAuthorArray){
         if (Arrays.asList(targetAuthorArray).contains(targetAuthor)) {
            targetAuthorArray[index] = String.valueOf(value);
         }
      }
      return targetAuthorArray;

   }

Here is the test that runs

@Test 
   public void getItemsByAuthorZeroMatchesTest() {
      createListForTesting(mediaList);
      String[] items = mediaList.getAllItemsByAuthor("No Author Matches This"); 
      assertEquals("Test 36: Test the getAllItemsByAuthor method with zero matches.", 0, items.length);
   }

When this test runs an error pops up

java.lang.ClassCastException: class Song cannot be cast to class java.lang.String (Song is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')

I have another method thats supposed to do the same thing. It is supposed to return a new array that contains String representation of all MediaItems whose title matches the targetTitle passed in. It is supposed to use its toString() method and cannot return any null values

Here is my code

 public String[] getAllItemsByTitle(String targetTitle){
      String targetTitleArray[] = new String[itemList.size()];
      int index = 0;
      for (MediaItem value : itemList){
         if (Arrays.asList(itemList).contains(targetTitle)) {
            targetTitleArray[index] = String.valueOf(value);
         }
      }
      return targetTitleArray;
   }

I didn't use the toString method because I didn't know where to incorporate it but here is the code for it

public String toString(){
     return title+", "+author+", "+genre;
  }

When getAllItemsByTitle runs it is supposed to return two matches but returns every item.

adenn
  • 1
  • 2

1 Answers1

0

targetAuthorArray[i] = (String)tempArray[i];

Your tempArray array appears to contain Song objects (you know you can also do Song[] array = new Song[size], right?).

Song cannot be converted into a String. Think of it like this:

int i = "ABCD";

It obviously won't work. Instead, do something like this:

tempArray[i].toString();

But then you encounter this problem, where it is also solved there. How do I print my Java object without getting "SomeType@2f92e0f4"?

FailingCoder
  • 757
  • 1
  • 8
  • 20
  • 1
    If you call `toString()` then you don't need the cast. – khelwood Dec 02 '19 at 01:56
  • My code contains subclasses such as Song, Book, Movie, and Podcast so the tempArray is supposed to hold all of those objects which is why I can't do `Song [] array = new Song[size]` when I do `tempArray[I].toString();` it compiles but still returns 11 items instead of 2 when the test runs – adenn Dec 02 '19 at 19:33
  • @adenn right before that loop, put `System.out.println(tempArray.length)` and see the length of the tempArray. Also, can you provide sample input and output for your code? – FailingCoder Dec 02 '19 at 20:28
  • The length of the tempArray is 11. I think there is something wrong with the second for loop. With 11 inputs such as Song: Money, Cardi B, hip hop, 3.03, Tiger Woods, Atlantic. Movie : 2001, A Space Odyssey, Kubrick, sci fi, 142, Keir Dullea, 1968. etc,. the `targetAuthorArray` correctly copies these but with the second for loop, when it goes to the if statement, it never goes inside to `targetTitleArray[index] = String.valueOf(value);` when I run `System.out.print(targetAuthorArray);`, Ljava.lang.String;@21bcffb5 prints 11 times even though I did my toString() method like the one you linked. – adenn Dec 02 '19 at 21:27
  • yes, and it's not working. my toString() methods are formatted the same way as that example. Here is one of the subclass toString methods: `@Override public String toString(){ return ("Book: " + super.toString() + ", " + numPages + ", " + fontSize); }` – adenn Dec 02 '19 at 21:41
  • Edit your question to include your edited code. Seriously. @adenn – FailingCoder Dec 02 '19 at 22:48
  • Oh yeah, including `super.toString()` might be a problem. That object should override toString as well. – FailingCoder Dec 03 '19 at 20:34