-2
java:19: error: no suitable method found for println(String,double)
   System.out.println("The health is %.2f",kiloliters);
             ^
    method PrintStream.println() is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(boolean) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(char) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(int) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(long) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(float) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(double) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(char[]) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(String) is not applicable
      (actual and formal argument lists differ in length)
    method PrintStream.println(Object) is not applicable
      (actual and formal argument lists differ in length)
2 errors
if(kiloliters > 1000 && kiloliters < 10000){ //test if the inputted number is in between 1000 and 10000
   kiloliters = (kiloliters + 150)/140;
   System.out.println("The health is %.2f",kiloliters);
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • 2
    If you want to print by providing `format` containing placeholders and `values` for them as other parameters use `System.out.format` or `System.out.printf`. – Pshemo Nov 23 '21 at 21:10
  • 1
    `System.out.printf("The health is %.2f%n", kiloliters);` Mind the `%n` newline. – Joop Eggen Nov 23 '21 at 21:12
  • Possibly related: [JAVA function like printf of C](https://stackoverflow.com/q/31645322) – Pshemo Nov 23 '21 at 21:16

2 Answers2

3

Java println() is the original "print text" method. You can use it like this:

System.out.println("The health is " + kiloliters);

printf() is a newer addition to Java; it allows "formatted printing" like C, C++ and C# (among many others). You can use it like this:

System.out.printf("The health is %.2f",kiloliters);

Yet another option is String.format():

System.out.println(String.format("The health is %.2f",kiloliters));

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • ... or since Java 15 non-static [`String#formatted​(Object... args)`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#formatted(java.lang.Object...)) for instance `System.out.println("The health is %.2f".formatted(kiloliters));`. – Pshemo Nov 23 '21 at 21:21
0

You should use System.out.format() instead:

System.out.format("The health is %.2f\n",kiloliters);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35