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
Eugenio Villarreal
8,041 PointsError: Exception in thread "main" java.util.IllegalFormatFlagsException: Flags = ' '
I can't get around this error: I searched online but could not figure out the issue.
Exception in thread "main" java.util.IllegalFormatFlagsException: Flags = ' '
at java.util.Formatter$FormatSpecifier.checkText(Formatter.java:3037)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2733)
at java.util.Formatter.parse(Formatter.java:2560)
at java.util.Formatter.format(Formatter.java:2501)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Example.main(Example.java:22)
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;
public class Example {
public static void main(String[] args) {
Treet[] treets = Treets.load();
System.out.printf("There are %d treets. %n",
treets.length);
Set<String> allHashTags = new HashSet<String>();
Set<String> allMentions = new HashSet<String>();
for (Treet treet : treets){
allHashTags.addAll(treet.getHashTags());
allMentions.addAll(treet.getMentions());
}
System.out.printf("Hash tags: s% %n", allHashTags);
System.out.printf("Mentions: s% %n", allMentions);
}
}
package com.teamtreehouse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class Treet implements Comparable<Treet>, Serializable {
private static final long serialVersionUID = 7146681148113043748L;
private String mAuthor;
private String mDescription;
private Date mCreationDate;
public Treet(String author, String description, Date creationDate) {
mAuthor = author;
mDescription = description;
mCreationDate = creationDate;
}
@Override
public String toString() {
return String.format("Treet: \"%s\" by %s on %s",
mDescription, mAuthor, mCreationDate);
}
@Override
public int compareTo(Treet other) {
if (equals(other)) {
return 0;
}
int dateCmp = mCreationDate.compareTo(other.mCreationDate);
if (dateCmp == 0) {
return mDescription.compareTo(other.mDescription);
}
return dateCmp;
}
public String getAuthor() {
return mAuthor;
}
public String getDescription() {
return mDescription;
}
public Date getCreationDate() {
return mCreationDate;
}
public List<String> getWords() {
String[] words = mDescription.toLowerCase().split("[^\\w#@']+");
return Arrays.asList(words);
}
public List<String> getHashTags(){
return getWordsPrefixedWith("#");
}
public List<String> getMentions(){
return getWordsPrefixedWith("@");
}
private List<String> getWordsPrefixedWith(String prefix) {
List<String> result = new ArrayList<String>();
for (String word : getWords()) {
if(word.startsWith(prefix)){
result.add(word);
}
}
return result;
}
}
1 Answer
Simon Coates
28,695 Pointsdocumentation on the printf method (https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...) ) includes mention that it can throw:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
So check your placeholders, and then test that they are appropriate for the arguments passed in.
Update. I ran a quick demo:
import java.util.HashSet;
import java.util.Set;
class Main {
public static void main(String[] args) {
Set<String> allHashTags = new HashSet<String>();
allHashTags.add("fun");
allHashTags.add("mercury");
System.out.printf("Hash tags: %s %n", allHashTags);
System.out.println("Or maybe something like");
int n = 1;
for(String tag : allHashTags)
{
System.out.printf("%d) %s %n", n, tag);
n++;
}
}
}
produces:
Hash tags: [mercury, fun]
Or maybe something like
1) mercury
2) fun
Eugenio Villarreal
8,041 PointsEugenio Villarreal
8,041 PointsHa, I thought I checked it but once again I miss-typed %s , I typed s%. This has become a common mistake for me. Thanks for pointing it out, I ket reading through it.
Simon Coates
28,695 PointsSimon Coates
28,695 PointsJava's standard library has good documentation compared to other programming languages and the exceptions are usually a decent indication of what's gone wrong. Getting placeholders wrong is an easy mistake to make (I always forget which symbol it's meant to be).