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

no error but its not working

Hi everyone, I create an example for myself, I have no error in javac but its not run and I got following error. my example:

import java.io.Console;
//my file is Index.java
public class Index{
    public static void main(String[] args){
        Console con = System.console();
        String first  = "pooya";
        String second = "pooya";
        String third  = "POOYA";
        // first if between first and second
        if(first.equals(second)){
            con.printf("they are 100% same");
        }
        if(first.equalsIgnoreCase(third)){
            con.printf("first and second are also same but ignoring case");
        }
    }
}

and my error is :

Exception in thread "main" java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags =  
    at java.util.Formatter$FormatSpecifier.failMismatch(Formatter.java:4298)
    at java.util.Formatter$FormatSpecifier.checkBadFlags(Formatter.java:2997)
    at java.util.Formatter$FormatSpecifier.checkGeneral(Formatter.java:2955)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2725)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.util.Formatter.format(Formatter.java:2455)
    at java.io.Console.format(Console.java:170)
    at java.io.Console.printf(Console.java:209)
    at Index.main(Index.java:11)
Ken Alger
Ken Alger
Treehouse Teacher

Updated for code formatting.

1 Answer

andren
andren
28,558 Points

The problem is likely the string "they are 100% same", when you use printf your string will be read through and formatted based on the placeholders it contains, for example if your string has a %s it will be replaced with a string variable you specify. As I assume you have seen examples of previously in the course.

The problem is that due to the fact that the % symbol is used to mark the beginning of a placeholder marker you can't use the % symbol in your text by itself without "escaping" it. Which basically means that you tell printf to treat the percentage as a literal percentage sign rather than the start of a placeholder.

To do so you simply place another % sign before your first one like this:

"they are 100%% same"

This will produce the desired output, though ideally if you don't use any placeholders in your string then you can print the line using the println method instead of the printf method. Then you won't have to worry about what characters you place in your string.