Candy Higgins
Candy Higgins

Reputation: 3

Syntax error in Celsius-to-Fahrenheit converter program

I have to allow the user to choose which conversion to do, and what degree to start at, then print the conversion starting at that degree up to the next 9 (10 conversions). And I have to write this using a callback function. Also there is a menu in the beginning. I have an "else without if" at line . Also my callback function is not calculating numbers correctly. How do I resolve this error and fix my callback function?

import java.util.Scanner;

public class FahrToCel {

    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        double choice = 0;
        double fahrenheit = 0;
        double celsius = 0;
        double answer = 0;


        //print the welcome message and the menu options for the user
        System.out.printf("Welcome to Candace's Fahrenheit and Celsius "
            + "convertor. ");
        System.out.printf("Please choose one of the following options: \n"
            + " Press 0 to exit \n Press 1 to to convert to Celsius \n"
            + " Press 2 to convert to Fahrenheit >  ");
        choice = input.nextInt();

        //print what happens when the user hits a button
        if (choice == 0){ 
            // the user is going to exit the game
            System.out.printf("Well hopefully you can play again soon! :) ");
        } else {
            //perform code for the operation
            for ( double i = fahrenheit; i >-100; i-=10 ){
                if (choice == 1) { 
                    // it will convert from fahrenheit to celsius
                    System.out.printf("Please pick a number in Fahrenheit: > ");
                    fahrenheit = input.nextDouble();
                    celsius = FahrToCel();
                    System.out.printf("%f ", i);
                }
            }
            for ( double i = celsius; i >-10; i-=10 ){
                if (choice == 2){ 
                    // it will convert from celsius to fahrenheit
                    System.out.printf("Please pick a number in celsius: > ");
                    celsius = input.nextDouble();
                    fahrenheit = CelToFahr();
                    System.out.printf("%f ", i);
                }
            } 

        } else {
            System.out.printf("I'm sorry, I did not ask you to enter that "
                + "number. ");
        }
    }

    public static double CelToFahr(){
        double celsius = 0;
        double fahrenheit = 0;
        celsius = (fahrenheit - 32) * (5.0/9.0);
        return celsius;
    }

    public static double FahrToCel( ){
        //perform the conversion for fahrenheit to celsius
        double celsius = 0;
        double fahrenheit = 0;
        fahrenheit = ((9.0/5.0) + 32) * celsius;
        return fahrenheit;

    }

}

Upvotes: 0

Views: 114

Answers (2)

DSlomer64
DSlomer64

Reputation: 4283

Among other things, (EDIT) ONE OF your functions is not quite right. Since they involve math, I'm offering my revisions instead of hints.

I don't know if you want to pass in the value you're converting from, but doing so would make it better.

You definitely don't want to set both celsius and fahrenheit to zero in both functions.

public static double FahrToCel(double fahrenheit ){
   double celsius;
   celsius = (fahrenheit - 32) * (5.0/9.0);
   return celsius;
 }

public static double CelToFahr( double celsius){
    //perform the conversion for fahrenheit to celsius
    double fahrenheit;
    fahrenheit = ((9.0/5.0) ) * celsius + 32;
    return fahrenheit;

This would mean that inside your for loops, the calls to the functions would have to be something like:

            celsius = input.nextDouble();
            fahrenheit = CelToFahr(celsius);

But I'm not sure you should input the next value of celsius, if I'm reading the instructions and intent of the for loops correctly.

Maybe you want this instead of inputting:

celsius = i;

Upvotes: 0

takendarkk
takendarkk

Reputation: 3443

You are looking for a structure like this

if (choice == 0){
    // ...
}
else if (choice == 1) {
    for ( double i = fahrenheit; i >-100; i-=10 ){
        // ...
    }
}
else if (choice == 2) {
    for ( double i = celsius; i >-10; i-=10 ){
        // ...
    }
}
else {
    // ...
}

Right now you have some if statements inside of an else and your scope is getting confused. Your loops would then go inside those else if statements. Also note that you are doing the conversion many times, but printing the initial value entered each time, not the converted temperature.

Upvotes: 1

Related Questions