Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Building the Model

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

I get Missing.Format.ArgumentExceptions, my code is exactly the same I uderstand everyting I am frustrated and lost :(

Could someone please take a look at my code. I get the below errors.

Adding Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s' at java.util.Formatter.format(Formatter.java:2519)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at com.teamtreehouse.model.Song.toString(Song.java:29)
at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2886)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2763)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Karaoke.main(Karaoke.java:16)

I had to change the songBook() method, I was being told that I had to declare a retunr value.

Javier MARQUEZ
Javier MARQUEZ
11,877 Points
import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;



public class Karaoke{

  public static void main(String[] args){

    Song song = new Song(     //remember the "new"  in constructors
      "Michael Jackson",
      "Beat it",
      "https://www.youtube.com/watch?v=pvdrN3GJFZo");

    SongBook songBook = new SongBook();
    System.out.printf("Adding %s %n", song);
    songBook.addSong(song);
    System.out.printf("The number of songs is: %d. %n", songBook.getSongCount());



  }



}
Javier MARQUEZ
Javier MARQUEZ
11,877 Points
package com.teamtreehouse.model;

public class Song{  
  private String mTitle;
  private String mArtist;
  private String mVideoUrl;


  public Song(String artist, String title, String videoUrl){
    mArtist = artist;
    mTitle = title;
    mVideoUrl = videoUrl;    
  }

  public String getTitle(){
    return mTitle;
  }

  public String getArtist(){
    return mArtist;
  }

  public String getVideoUrl(){
    return mVideoUrl;
  }

  @Override
  public String toString(){
    return String.format("Title : %s by %s, click here: %s", mTitle, mArtist);
  }


} ```
Javier MARQUEZ
Javier MARQUEZ
11,877 Points
 package com.teamtreehouse.model;

import java.util.List; //importing the list interface
import java.util.ArrayList;

public class SongBook{
  private List<Song> mSongs;      //empty interface "mSomething" is private

  public void songBook(){
    mSongs = new ArrayList<Song>(); //remember the "new" word
  }

  public void addSong(Song song){
    mSongs.add(song);
  }

  public int getSongCount(){
    return mSongs.size();
  }

} ```

2 Answers

Simon Coates
Simon Coates
28,694 Points

can you try

return String.format("Title : %s by %s, click here: ", mTitle, mArtist);

think it might be expecting a fourth argument due to three instances of %s

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

And you were right, the string format was waiting for 3 pieces of date. Thanks you so much

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

Hello I got it.

I was having an error with the constructor of the Songbook.

I had a typo here:

public songBook(){
    mSongs = new ArrayList<Song>(); //remember the "new" word
  }

//Constructors have the same name of the class file, CaSeSeNsItIvE!!!

  public SongBook(){
    mSongs = new ArrayList<Song>(); //remember the "new" word
  }