Home > Software engineering >  error: no suitable method found for println(String,float)? method PrintStream.println(float) is not
error: no suitable method found for println(String,float)? method PrintStream.println(float) is not

Time:11-04

I was trying to print (int, boolean, char, double) in a same println statement .

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);

        float num1 = input.nextFloat();
        float num2 = input.nextFloat();
        float num3 = input.nextFloat();
        float num4 = input.nextFloat();

        if(num1 <= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
            float equal = num1   num2   num3   num4;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
            float equal = num1   num3   num4;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
            float equal = num1   num2   num4;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
            float equal = num1   num2   num3;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
            float equal = num1;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
            float equal = num2;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
            float equal = num3;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
            float equal = num4;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
            float equal = num1   num2;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
            float equal = num1   num3;
            System.out.println("%.2f", equal);
        } else if(num1 <= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
            float equal = num1   num4;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
            float equal = num2   num3;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
            float equal = num2   num4;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
            float equal = num3   num4;
            System.out.println("%.2f", equal);
        } else if(num1 >= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
            float equal = 0.00;
            System.out.println("%.2f", equal);
        }
    }
}

I don't understand why this won't work. Please explain me this.

CodePudding user response:

The type of System.out is PrintStream. There are several overeloads of the method println(), but what they all have in common is that they take either no or a single parameter.

The method you are looking for - that takes a format string and multiple values to be inserted - is PrintStream.format():

public PrintStream format(String format, Object... args)

Writes a formatted string to this output stream using the specified format string and arguments.

CodePudding user response:

You can use System.out.printf instead of System.out.println, using the String %.2f before of the value that you want to print, as you already did in your code:

`
if(num1 <= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
            float equal = num1   num2   num3   num4;
            System.out.printf("%.2f", equal);
        }
`
  • Related