Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Char/String/Int Trouble

$
0
0
I am currently trying to make a temperature converter for my class on programming and I'm having one, nagging problem. For the user to be prompted for what temperature they need converted, they must first specify whether it is Celsius or Fahrenheit. Our professor specified that the user is to enter either c, C, f, or F. I'm having trouble with what to make this valuable, double or String. When I try either, I get errors. I tried int last time on a whim and there were no visible errors, but I got them after running.

import java.util.Scanner;

public class TemperatureConverter 
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		System.out.print("Please enter the type of temperature conversion you need to perform: ");
		int letter = in.nextInt();
		
		if (letter == 'c' || letter == 'C')
		{
			System.out.print("Please enter the Celsius temperature to be converted to Fahrenheit: ");
			double originalC = in.nextDouble();
			double convertedF = (originalC / (5/9) + 32);
			System.out.print("The equivalent temperature in Fahrenheit is " + convertedF);
		}
		if (letter == 'f' || letter == 'F')
		{
			System.out.print("Please enter the Fahrenheit temperature to be converted to Celsius: ");
			double originalF = in.nextDouble();
			double convertedC = ((5/9) * (originalF - 32));
			System.out.print("The equivalent temperature in Celsius is " + convertedC);
		}
		else 
		{
			System.out.print("Invalid conversion, must enter either C or F, program will exit.");
		}
	}

	
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>