1

I am getting this type of error in my eclipse software :

Question

Why do I get an error?

code :

    package loops;
    public class Escapey {
        public static void main(String[] args) {
            String name ="micheal";
            System.out.printf("i am %s, my friend name also %s",name);
        }
    }

Error message:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s' at java.base/java.util.Formatter.format(Formatter.java:2672) at java.base/java.io.PrintStream.format(PrintStream.java:1053) at java.base/java.io.PrintStream.printf(PrintStream.java:949) at loops.Escapey.main(Escapey.java:5)

  • Ouput excepted :(
Philipp
  • 2,787
  • 2
  • 25
  • 27
Micheal
  • 11
  • 4

5 Answers5

4

You can either specify the argument twice, or specify an index when referenced in the format string:

System.out.printf("i am %1$s, my friend's name also %1$s", name);
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
  • @l'L'l is `%0` actually guaranteed behaviour? I can't see it in the documentation: all of the examples start at 1. – Andy Turner Feb 05 '19 at 07:26
  • @l'L'l I'm not asking you to demonstrate that it works: I see that it does. That wasn't the point: I'm questioning whether you should rely upon undocumented behaviour. – Andy Turner Feb 05 '19 at 08:20
  • @AndyTurner: *No*, undocumented behavior shouldn't be relied upon. It is odd that `javac` doesn't even warn about it though; the `C` compiler does. I'm pretty sure that `Java` simply treats `%0$s` as `%s`, (`%000000$s` is no different). – l'L'l Feb 05 '19 at 09:25
  • "or you could do" don't recommend things you shouldn't rely on without a big warning! – Andy Turner Feb 05 '19 at 11:24
  • @AndyTurner: I learned this information through our discussion here. I'm not a compiler — that is where the warning should have come from! I can't edit what I wrote earlier; I've removed it, although the comments that follow don't make much sense, so you might consider removing your comments as well. – l'L'l Feb 05 '19 at 19:14
  • @AndyTurner Hello Andy.....could you suggest me best site to learn java ?? I already learn some basic in java...I dont know where should i learn after java ... – Micheal Feb 07 '19 at 14:50
3

By having two %s the printf is expecting two arguments

like

System.out.printf("i am %s, my friend's name also %s",name, name);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

One solution,

System.out.print("i am " + name + ", my friend name also " + name); // If you want to continue on same line 
System.out.println("i am " + name + ", my friend name also " + name); // If you want to continue on next line 

Since you have two %s, two arguments are expected, so either you specify two arguments.

System.out.printf("i am %s, my friend name also %s",name, name);

Or specify an index in the string. %1$s will get the first argument, in this case name.

System.out.printf("i am %1$s, my friend's name also %1$s", name);

Read more here! :)

Java printf( ) Method Quick Reference

Java Format - Java printf Value Index // explicit indexing, relative indexing

Strazan
  • 147
  • 1
  • 16
  • System.out.print("i am %s, my friend name also %s",name); | Error: | no suitable method found for print(java.lang.String,java.lang.String) – Scary Wombat Feb 05 '19 at 06:05
  • @Micheal explicit indexing! I have updated my answer with good links you will find very helpful I reckon ( second one especially ). – Strazan Feb 05 '19 at 06:27
  • 1
    wow you are great, It works now :) Thanks for the link too :) :) – Micheal Feb 05 '19 at 06:44
  • no problem! consider verifying this answer, so others with the same problem easily finds a solution :) – Strazan Apr 16 '19 at 12:38
1

use this ...

System.out.printf("i am %s, my friend name also %s",name, name);

You are giving two specifiers. So you need to give name twice here.

Mukit09
  • 2,956
  • 3
  • 24
  • 44
1

A pretty obscure way to do this:

System.out.printf("i am %s, my friend's name also %<s", name);

I have literally never used this, but it is mentioned in the documentation of Formatter.

Another way to reference arguments by position is to use the '<' ('\u003c') flag, which causes the argument for the previous format specifier to be re-used.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243