package farecalc;

import java.io.*;
public class FareCalc   {
    public static void main (String[] args) throws IOException    {


     BufferedReader in;
    in = new BufferedReader(new InputStreamReader(System.in));

     double firstMile = 2.80;
     double otherMile = 1.20;
            System.out.println("Please insert your total Mileage");
            String mileageInput = in.readLine();
            double mileage = Integer.parseInt(mileageInput);
      
            double price = firstMile + (otherMile * (mileage - 1));
          
                    System.out.println("Your fare is £" + price);

    }



}

what it should do is take a mileage and work out a cost to it. e.g. 2miles = £4
it works if its a whole number but not if there is a decimal e.g. 2 works 2.1 doesn't.

can anyone help?

thanks

Edited 7 Years Ago by tuffyg: n/a

package farecalc;

import java.io.*;
/*
public class FareCalc   {
    public static void main (String args[]) throws IOException    {


     BufferedReader in;
    in = new BufferedReader(new InputStreamReader(System.in));

     double firstMile = 2.80;
     double otherMile = 1.20;
            System.out.println("Please insert your total Mileage");
            String mileageInput = in.readLine();
            double mileage = Integer.parseInt(mileageInput);
      
            double price = firstMile + (otherMile * (mileage - 1));
          
                    System.out.println("Your fare is £" + price);

    }



}

what it should do is take a mileage and work out a cost to it. e.g. 2miles = £4
it works if its a whole number but not if there is a decimal e.g. 2 works 2.1 doesn't.

can anyone help?

thanks

I have made the correction please check it whether it will work or not......... your welcome...... :)

On line 15, you are parsing the user input to an integer, and integers do not function with decimals. Parse the input string to a double:

double mileage = Double.parseDouble(mileageInput);

On line 15, you are parsing the user input to an integer, and integers do not function with decimals. Parse the input string to a double:

double mileage = Double.parseDouble(mileageInput);

hi thank you both for your replies
1. mehaboob i dont know what you did but it didn't work (but thank you for trying)
2. majestic.
.it ..... worked . Thanks

i tried putting

double mileage = int.parseInt(mileageInput);

before and it didn't work. i kicked myself when i found out what i did wrong lol.

thanks

This question has already been answered. Start a new discussion instead.