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 - Retired Organizing Data Interfaces

Eugenio Villarreal
Eugenio Villarreal
8,041 Points

Exception in thread "main" error : conversion = '%'

I'm getting this error: This is a new Treet: Exception in thread "main"

 java.util.UnknownFormatConversionException: Conversion = '%'                
        at java.util.Formatter.checkText(Formatter.java:2579)                                                               
        at java.util.Formatter.parse(Formatter.java:2565)                                                                   
        at java.util.Formatter.format(Formatter.java:2501)                                                                  
        at java.util.Formatter.format(Formatter.java:2455)                                                                  
        at java.lang.String.format(String.java:2940)                                                                        
        at com.teamtreehouse.Treet.toString(Treet.java:18)                                                                  
        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 Example.main(Example.java:17)  

Here is my code:

Example.java

import java.util.Arrays;
import java.util.Date;
import com.teamtreehouse.Treet;


public class Example{

  public static void main(String[] args){
    Treet treet = new Treet(
      "eugeniovillarreal",
      "Twitter confirms it’s testing a feature that allows users to more easily " + 
      "create “tweetstorms” – those series of connected tweets that have grown to be a popular" +
      "workaround for Twitter’s character count limitations.",
      new Date(1447979631L)
    );

    System.out.printf("This is a new Treet: %s %n",treet);

    Treet secondTreet = new Treet(
      "bobbyblue",
      "ATwitter confirms it’s testing a feature that allows users to more easily " + 
      "create “tweetstorms” – those series of connected tweets that have grown to be a popular" +
      "workaround for Twitter’s character count limitations.",
      new Date(1511550530L)
    );

    System.out.printf("This is a new Treet: %s %n",secondTreet);
    System.out.println("The words are:");
    for (String word : treet.getWords()) {
      System.out.println(word);
    }

    Treet[] treets = {treet, secondTreet};

    Arrays.sort(treets);
    for (Treet exampleTreet : treets){
      System.out.println(exampleTreet);
    }

  }
}
Treet.java

package com.teamtreehouse;
import java.util.Date;

public class Treet implements Comparable{
  private String author;
  private String description;
  private Date creationDate;  


  public Treet(String author, String description, Date creationDate){
    this.author = author;
    this.description = description;
    this.creationDate = creationDate;
  }

  @Override
  public String toString(){
    return String.format( "Treet: \"%s\" by %s on s%",
                         description, author, creationDate);
  }

  @Override
  public int compareTo(Object obj){
    Treet other = (Treet) obj;
    if(equals(other)){
      return 0;
    }
    int dateCmp = creationDate.compareTo(other.creationDate);
    if (dateCmp == 0){
      return description.compareTo(other.description);
    }
    return dateCmp;
  }

  public String getAuthor() {
    return author;
  }

  public String getDescription(){
    return description;
  }

  public Date getCreationDate(){
    return creationDate;
  }

  public String[] getWords(){
    return description.split("[^\\w@#']+");
  }
}

1 Answer

 @Override
  public String toString(){
    return String.format( "Treet: \"%s\" by %s on s%",
                         description, author, creationDate);
  }

You are swapped the order of the letter s with the sign % on the last one.

Eugenio Villarreal
Eugenio Villarreal
8,041 Points

Whaaaaaaat! :s So silly, I totally kept missing that! Now it works just fine. Thanks!