user3544721
user3544721

Reputation: 61

Java program not showing output

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

Upvotes: 1

Views: 6163

Answers (4)

MadProgrammer
MadProgrammer

Reputation: 347332

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

Upvotes: 1

Alfred Morgan
Alfred Morgan

Reputation: 1

I would suggest using:

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

Rather than printf, since you are not using placeholders .

Upvotes: 0

Joseph Furfaro
Joseph Furfaro

Reputation: 1

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

Upvotes: 0

Eran
Eran

Reputation: 394156

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

Upvotes: 3

Related Questions