0

I am trying to create a code that will prompt the user for input and allow them to convert between different kinds of metric and US measurements. I thought I was finished but I encountered an error that I couldn't fix. Every time I used "Double.valueOf(readConsole.next());" I got an error that said "type mismatch: cannot convert from Double to double." Any ideas on how to fix this? Any help is much appreciated.

My code:

import java.util.Scanner;

public class MeasurementConversion
{
    public static void main(String[]args)
    {
        Scanner readConsole = new Scanner(System.in);
        String convert;
        double poundAmount;
        double kilogAmount;
        double ounceAmount;
        double gramAmount;
        double feetAmount;
        double meterAmount;
        double mileAmount;
        double kilomAmount;

        System.out.println("Hello, I am a conversion calculator. I can convert pounds to kilograms, ounces to grams,");
        System.out.println("feet to meters, miles to kilometers, and vice versa.");
        System.out.println("What would you like to convert?");
        convert = readConsole.nextLine();
        if (convert.equalsIgnoreCase("pounds to kilograms"))
        {
            System.out.println("Ok, what is the amount of pounds you know?");
            poundAmount = Double.valueOf(readConsole.next());
            kilogAmount = poundAmount * .4536;
            System.out.println("That is " + kilogAmount + " kilograms.");
        }
        if (convert.equalsIgnoreCase("kilograms to pounds"))
        {
            System.out.println("Ok, what is the amount of kilograms you know?");
            kilogAmount = Double.valueOf(readConsole.next());
            poundAmount = kilogAmount * 2.20462;
            System.out.println("That is " + poundAmount + " pounds.");
        }
        if (convert.equalsIgnoreCase("ounces to grams"))
        {
            System.out.println("Ok, what is the amount of ounces you know?");
            ounceAmount = Double.valueOf(readConsole.next());
            gramAmount = ounceAmount * 28.5;
            System.out.println("That is " + gramAmount + " grams.");    
        }
        if (convert.equalsIgnoreCase("grams to ounces"))
        {
            System.out.println("Ok, what is the amount of grams you know?");
            gramAmount = Double.valueOf(readConsole.next());
            ounceAmount = gramAmount * .035274;
            System.out.println("That is " + ounceAmount + " ounces.");
        }
        if (convert.equalsIgnoreCase("feet to meters"))
        {
            System.out.println("Ok, what is the amount of feet you know?");
            feetAmount = Double.valueOf(readConsole.next());
            meterAmount = feetAmount * .3048;
            System.out.println("That is " + meterAmount + " meters.");
        }
        if (convert.equalsIgnoreCase("meters to feet"))
        {
            System.out.println("Ok, what is the amount of meters you know?");
            meterAmount = Double.valueOf(readConsole.next());
            feetAmount = meterAmount * 3.28084;
            System.out.println("That is " + feetAmount + " feet.");
        }
        if (convert.equalsIgnoreCase("miles to kilometers"))
        {
            System.out.println("Ok, what is the amount of miles you know?");
            mileAmount = Double.valueOf(readConsole.next());
            kilomAmount = mileAmount * 1.61;
            System.out.println("That is " + kilomAmount + " kilometers.");
        }
        if (convert.equalsIgnoreCase("kilometers to miles"))
        {
            System.out.println("Ok, what is the amount of kilometers you know?");
            kilomAmount = Double.valueOf(readConsole.next());
            mileAmount = kilomAmount * .621371;
            System.out.println("That is " + mileAmount + " miles.");
        }
    }
}
cpitt6477
  • 51
  • 1
  • 6
  • 6
    Why not use `readConsole.nextDouble()` instead of`Double.valueOf(readConsole.next())`? – VatsalSura Aug 09 '16 at 18:45
  • Can not reproduce. Are you perhaps defining your own class called `Double`? – Jorn Vernee Aug 09 '16 at 18:57
  • Same here. I downloaded your code; compiled it (fine); and ran it; and besides the fact that your code is hard to use; everything works. I tried various conversions; and unless i give a really invalid number (like a string as blablub) everything works fine. So please be more specific; provide the exact input that leads to your error. – GhostCat Aug 09 '16 at 19:26
  • Then, on code quality: why are asking the user to enter a whole string? Just print a list like "1 - miles to kilometer; 2 - kilometer to miles ... " and ask the user to enter that number. Less work than he typing strings. And much less chance of providing bad input! – GhostCat Aug 09 '16 at 19:27

2 Answers2

0

I am not a java expert, but from the code I see that your variable is of primitive type double whereas your method call returns an instance of the class Double. You can use Double.parseDouble() to convert a String to a double. Check Convert String to double in Java

Community
  • 1
  • 1
b3rt0
  • 769
  • 2
  • 6
  • 21
0

I think you compile in Java 1.4 or less and that you have a problem not at the execution but during the compilation.

type mismatch: cannot convert from Double to double.

is a compilation error.

Java 1.4 and less don't support the autoboxing. So, the auto-conversionDouble -> double you typed is not allowed here :

 double poundAmount = Double.valueOf(readConsole.next());

Try to check the Java version used and use at least the 1.5 version of the JDK

davidxxx
  • 125,838
  • 23
  • 214
  • 215