简体   繁体   中英

Java println method error

I am trying to print times that it takes to do certain methods. I use

double t0 = 0.01 * System.currentTimeMillis(); I have 3 times like that one. And when I print it like this:

System.out.println("%7d    %10.7f       %10.7f", amount,
      (t1 - t0), (t2 - t1));

I get an error:

no suitable method found for println(String,int,double,double)

Why is that? Where does that string parameter come from?

You used System.out.println() which doesn't support adding arguments like you did in your program. Use System.out.printf() instead of that.

use this

String string = String.format("%7d    %10.7f       %10.7f", amount,
      (t1 - t0), (t2 - t1));
System.out.println(string);

reason: https://javapapers.com/core-java/system-out-println/

println(xxx) is an overloaded method from class PrintStream (java.io.PrintStream)

go to command Prompt and enter

javap java.io.PrintStream 

Then you get to see all the methods present inside the PrintStream class , try to find the method "println(String,int,double,double)" , you wont find such method .

if you want to print multiple values , use println(java.lang.String) to print the value of int a=5; int b=6;

System.out.println(a+" "+b);

the + operator is performing the string concatination and the whole expression in the () is consider as string .

you can also use the print() in java which behaves same like print() in c . System.out.print(xxx); , it is also overload method.

Instead of this statement -:

System.out.println("%7d    %10.7f       %10.7f", amount,
      (t1 - t0), (t2 - t1));

Use this -: concatenate to get the output

System.out.println("%7d    %10.7f       %10.7f"+ amount+
      (t1 - t0)+ (t2 - t1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM