1

This is the coding i have come up with for a simple car rent java program. it compiles with no error but the output is not showing, so what should i do? Please help tx. I am a java beginner.

import java.util.Scanner;
public class carRent
{
  public static void main(String args[])
  {
    Scanner input = new Scanner(System.in);
    String name, address;
    double bodo,endodo, kilo;
    double days, charge;

    System.out.print("Name: ");
    name=input.nextLine();
    System.out.print("State: ");
    address=input.nextLine();
    System.out.print("Beginning Odometer : ");
    bodo=input.nextDouble();
    System.out.print("End Odometer : ");
    endodo=input.nextDouble();
    System.out.print("Days car used : ");
    days=input.nextDouble();


    kilo = endodo - bodo;
    System.out.printf("\nKilometer car driven  : ", + kilo ,"km");
    System.out.printf("\nNumber of days rented : ", + days);
    charge = days * 150.00 + kilo * 1.00;
    System.out.printf("\nTotal charge of rent  : ", + charge);
    } 
}
3
  • System.out.printf("%nKilometer car driven :%f.2km", kilo); Commented Mar 14, 2015 at 4:33
  • how can i make the output to two decimal place? Commented Mar 14, 2015 at 4:58
  • That's what %.2f does...Take a look at this for more examples Commented Mar 14, 2015 at 5:00

4 Answers 4

3

You are not using printf correctly (since you didn't specify any place holders for the variables in the format String). I suggest you use println instead :

    kilo = endodo - bodo;
    System.out.println("\nKilometer car driven  : " + kilo + "km");
    System.out.println("\nNumber of days rented : " + days);
    charge = days * 150.00 + kilo * 1.00;
    System.out.println("\nTotal charge of rent  : " + charge);

Sample input and output :

Name: Eran
State: IL
Beginning Odometer : 1000
End Odometer : 2000
Days car used : 2

Kilometer car driven  : 1000.0km

Number of days rented : 2.0

Total charge of rent  : 1300.0
Sign up to request clarification or add additional context in comments.

5 Comments

how may i set the output to two decimal places?
@user3544721 Using this method, you need to use a NumberFormat, using printf, something like System.out.printf("%nKilometer car driven :%f.2km", kilo);
but it displays like eg 60.000000.2 which is weird. why is that?
Yeah sorry, it should be %0.2f :P
like for the days i would like t display as an integer how do i convert from double to integer?
1

printf is short for "print format" which takes a format String and parameters with which they need to be formatted...

Instead you should be using something more like...

System.out.printf("%nKilometer car driven  : %.2fkm", kilo);

Which can output something like...

Name: test
State: Test
Beginning Odometer : 123.456
End Odometer : 789.123
Days car used : 10.25

Kilometer car driven  : 665.67km
Number of days rented : 10
Total charge of rent  : 2203.17

For example of the values...

    System.out.printf("%nKilometer car driven  : %.2fkm", kilo);
    System.out.printf("%nNumber of days rented : %.0f", days);
    charge = days * 150.00 + kilo * 1.00;
    System.out.printf("%nTotal charge of rent  : %.2f%n", charge);

For more details, take a look at these examples

Comments

0

If you want to be able to add multiple Strings on one line use:

System.out.print("I am an example String!");

If you want the String to be on its own line use:

System.out.println("I am an example String on my own line!");

Comments

0

I would suggest using:

System.out.println("Insert String Here");

Rather than printf, since you are not using placeholders .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.