Java Converter Task

Please help.
What is wrong with my solution. The preparation platform doesn’t accept it, though IntelliJ IDEA runs it and gives the desired results.
The task is
Converter

You want to buy this really cool car from the UK, but you are worried about the fuel consumption. The values you see are MPG (miles per gallon). You have no idea what 20 MPG means so, being a programmer, decide to write a converter that helps you calculate the consumption.
Doing some research, you learn that 1 gallon = 4.54 litres and 1 mile = 1.6 km .
After the calculation, round the result down to the neareast whole number.
Input
On the first line you will receive a number m - miles per galon
Output
On the only line of output, print {result} litres per 100 km
Constraints
1 <= m <= 100
Input
20
Output
14 litres per 100 km
Input
44
Output
6 litres per 100 km

My solution:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner user_input= new Scanner(System.in);

int m = user_input.nextInt();

int result = (int) (282.48 / m);

System.out.print(Math.floor(result) + " " + "litres per 100 km");


}

}

Thank you for your time and help!

Your output on 20 is - 14.0 litres per 100 km , you have to remove everything after the point like this example - 14 litres per 100 km.
HINT: Try with String.format() or System.out.printf();

1 Like

Thanks Lyubo, although if I try string format then I can’t apply Math.floor which is requiered at the end
On the other hand when I try System.out.printf() the message I receive is " Redundant to call printf() "
I don’t know what to do

I tried to change the formula, using all the given numbers in the instructions. And now when I use Math.round the result is correct and gives me a whole number.
But the system still does not accept it.
Could you please take a look and see if you see anything wrong
Thank you so much!
The updated solution below:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner user_input= new Scanner(System.in);

    double m = user_input.nextDouble();
    double mile = 1.6;
    double gallon = 4.54;
    double result = (100 * gallon) / (mile * m);
    String lkm = "litres per 100 km ";

    System.out.print(Math.round(result) + " " + lkm);


}

}

Remove space at end of String(lkm) and try again. (space after “km” i mean)

just tried it.
The system still doesn’t accept it as correct answer, but at least is giving me 40 points.

Hello,
I do not think that you should use Math.round() here at all.

The task description says: “After the calculation, round the result down to the neareast whole number.”

The Math.round() method in Java is used to round a number to its​ closest integer. This is done by adding 1/2 to the number, taking the floor of the result, and casting the result to an integer data type.

E.g. if input is 11, the result is 25.795454545454543 and you should output 25, not 26 (which your code does).

So you can try with just casting the result to int >>

  System.out.print((int)result + " " + lkm);

Good luck:)

1 Like

Solved! Thank you so much!
You’re the best!

1 Like