JohnnyQuest1027
JohnnyQuest1027

Reputation: 1

java temperature conversion program not working

I have an assignment for an intro to programming class I'm taking, and I don't have any errors but it still isn't working properly. The assignment is to write a program that converts temperatures both from celsius to fahrenheit and vice versa. It also has to be done in different methods and must then loop back through to be done over and over. This part is where I'm now having a problem. the line

if(retry == y)

isn't working properly. Thanks in advance.

package cps101temperatureconverter;
import static 
cps101temperatureconverter.CPS101TemperatureConverter.enteredTemp;
import java.util.Scanner;

public class CPS101TemperatureConverter 
{

//char inputChoice;
int input;
static double enteredTemp;
static double calculatedTemp;
static char retry = 'y';
static Boolean moreToProcess = true;
static double fahrenheitTemp;
static double celsiusTemp;
Scanner keyboard = new Scanner(System.in);
static char c = 'C';
static char f = 'F';

public static void main(String[] args) 
{
    char inputChoice;
    Scanner keyboard = new Scanner(System.in);
    String inputTempString;
    String inputTypeString;
    String retryString;
    //char retry = 'y';

    do
    {


    System.out.println("This program will convert temperatures.");
    System.out.println("Please Enter a Temperature: ");
    inputTempString= keyboard.nextLine();
    enteredTemp = Double.parseDouble(inputTempString);
    System.out.println("You have entered " + enteredTemp);
    System.out.println("Is this value in (C)elsius or (F)ahrenheit? ");
    inputTypeString = keyboard.nextLine();
    inputChoice = inputTypeString.charAt(0);

    if (inputChoice == c)
    {
        CelsiusToFahrenheit();
    }
    else if (inputChoice == f)
    {
        FahrenheitToCelsius();
    }
    else
    {
        System.out.println("You have entered an invalid answer. Please try again.");
    }
    System.out.println("Would you like to convert another temperature? Enter yes or no. ");
    retryString = keyboard.nextLine();
    retry = retryString.charAt(0);
    if (retry == y)
    {
        moreToProcess = true;
    }
    else{
        moreToProcess = false;
    }
    }while(moreToProcess = true);

    System.out.println("The Program will terminate now.");

}

static void CelsiusToFahrenheit()
{
    calculatedTemp = (9.0/5.0)*enteredTemp + 32;
    System.out.println(enteredTemp + " converted to Fahrenheit is " + calculatedTemp);
}
static void FahrenheitToCelsius()
{
    calculatedTemp = (enteredTemp - 32)*(9.0/5.0);
    System.out.println(enteredTemp + " converted to Celsius is " + calculatedTemp);
}

}

Upvotes: 0

Views: 189

Answers (2)

Francisco Colmenares
Francisco Colmenares

Reputation: 21

The short answer is that you haven't initialized c or f. They are just static char variables but you have to give them a value to compare against.

static char c = 'C';
static char f = 'F';

Note however that this comparison is case sensitive; if the user inputs lower case c or f it won't work. You should try making your program more explicit as to what input it expects. Maybe like this:

System.out.println("Is this value in (C)elsius or (F)ahrenheit? ");

Upvotes: 2

Joel
Joel

Reputation: 138

Your fields called c and f are not defined. Try:

static char c = 'c';
static char f = 'f';

Good luck.

Upvotes: 0

Related Questions