zerociudo
zerociudo

Reputation: 377

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?

Upvotes: 0

Views: 5295

Answers (4)

harbinger
harbinger

Reputation: 134

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));

Upvotes: 0

Sai Kiran
Sai Kiran

Reputation: 74

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.

Upvotes: 0

adarsh
adarsh

Reputation: 142

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/

Upvotes: 2

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

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

Upvotes: 3

Related Questions