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

Recommended Answers

All 4 Replies

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

You are welcome, it's often the obvious things that we all miss! Please mark the thread as solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.