James D'Amico
James D'Amico

Reputation: 39

Celsius/Fahrenheit conversion error

sorry for my beginners mistakes, but I was coding a calculator that will convert fahrenheit to celsius and celsius to fahrenheit, but I keep getting this error:

Exception in thread "main" java.lang.NumberFormatException: 
For input string: "69C"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.valueOf(Double.java:504)
    at Converter.main(Converter.java:39)  

Here is my code, hope you can help:

import java.util.Scanner; 
public class Converter {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Welcome to the Fahrenheit/Celsius Converter!");
        System.out.println("To convert, type in your temperature and the
                            identify if you are using fahrenheit or celsius.");
        System.out.println("Example: 69F or 69C F = Fahrenheit C = Celsius");

        String temperature;
        char f = 'f';
        char c = 'c';
        double answer , temp;

        temperature = keyboard.nextLine();            

        if (temperature.contains("F")) {
            temperature = temperature.replace(f , ' ');
            temp = Double.valueOf(temperature);
            answer = temp - 30 / 2;
            System.out.println("Poof! The temperature is " + answer + "C");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("F")) {
            temperature = temperature.replace(f , ' ');
            temp = Double.valueOf(temperature);
            answer = temp - 30 / 2;
            System.out.println("Poof! The temperature is " + answer + "C");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("C")) {
            temperature = temperature.replace(c , ' ');
            temp = Double.valueOf(temperature);
            answer = temp + 30 * 2;
            System.out.println("Poof! The temperature is " + answer + "F");
        } else {
            System.out.println("You entered the temperature wrong!");
        }

        if (temperature.contains("c")) {
            temperature = temperature.replace(c , ' ');
            temp = Double.valueOf(temperature);
            answer = temp + 30 * 2;
            System.out.println("Poof! The temperature is " + answer + "F");
        } else {
            System.out.println("You entered the temperature wrong!");
        }
    }
}

Upvotes: 1

Views: 838

Answers (2)

John3136
John3136

Reputation: 29266

The error tells you everything you need to know - right down to the line number!. "69C" is not an integer, so when you try to parse it, it doesn't work!.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44844

you can not convert 69C to a number can you. The replace is case sensitive

try

temperature = temperature.replace('C' , ' ');

of course you can change your varaable to 'C'

Upvotes: 1

Related Questions