Hello,
I'm a student working on my first hands-on assignment. One of the questions is this:
Q4: Write a program that converts a user input temperature from degree Celsius to Fahrenheit or Fahrenheit to Celsius using:
C = 5 (F -32)/9 or F = 9C/5 + 32
The user must input temperature with a ‘c’ or ‘f’ (case insensitive) after the number and the program should output the corresponding value. If any other letter, is used, print an error. [10]

This is what I have so far, but it's returning 7 errors - one of them being "enum types must not be local", and the rest are about the illegal start of an expression, else without if, and that a semicolon was expected. I feel like I'm pretty close (except for the 7 errors!) Any pointers would be much appreciated. Thank you!

import java.util.*;
import java.util.Scanner;

public class TemperatureConverter{

    public static void main(String[] args){
        int temperature;
        enum TempType {C, c, F, F}
        System.out.println ("Enter a temperature in whole degrees only.");
        Scanner keyboard = new Scanner(System.in);
        temperature = keyboard.nextInt( );
        System.out.println ("Now enter an 'F' if the temperature is Fahrenheit or a 'C' if it is Celsius.");
        TempType = keyboard.nextChar( );
        if (TempType == 'c') | (TempType == 'C')
            System.out.println (temperature + " degrees Celsius equals " + (((temperature * 9)/5) + 32) + " degrees Fahrenheit.");
        else if (TempType == 'f') | (TempType == 'F')
            System.out.println (temperature + " degrees Fahrenheit equals " + (((temperature - 32) * 5) / 9) + " degrees Celsius.");
        else 
            System.out.println ("I'm sorry. You did not enter a 'C' or an 'F.'");
    }
}

Recommended Answers

All 3 Replies

Couple of problems with your code:
- Enum declarations as per the specification can't be local i.e. placed in a method. Move the enum declaration either outside of your TemperateConverter class or either outside the main() method.
- Why C, c and F, f (BTW, you have got both capital F) when in the end both C/c stand for the same thing? Just declare two members for your enum, C and F and you should be good to go.
- You can't directly convert between enum and strings, which is what you seem to be doing in TempType = in.nextChar() (which is BTW incorrect since TempType is a type name). Use valueOf() method of an enum which returns the enum object from string representation. Make sure you account for the case sensitivity of enums by passing in a stripped and upper-case version of the user entered string.
- No error handling when the user enters "A" as the temperature.

Thank you for your help. I used c, C, f, and F (or at least thought I did!) because I thought that was the only was to make it case insensitive. COuld you pleasse explain this a little, though? I'm such a newbie that I don't even know what you're saying. Thank you!

Use valueOf() method of an enum which returns the enum object from string representation. Make sure you account for the case sensitivity of enums by passing in a stripped and upper-case version of the user entered string.

OK, my point was:
The representation of C or F for the user can change based on how it is presented to the user. E.g. the user can enter "C" for celsius and tomorrow might want to enter "CELSIUS" etc. As far as your domain (problem statement) representation is concerned, there are only two temperature types (C and F). It is *your* responsibility (or the responsibility of the presentation/user interface) layer to convert the user entered value to C or F temp type i.e. how the user perceives Celsius or Fahrenheit shouldn't change how your *application* perceives the temp type.

To talk in terms of code (not compiled):

enum TempType { C, F }

public class Test {

    public static void main(final String [] args) {
        String line = scanner.nextLine().trim();

        // Account for case sensitivity here ---v
        // instead of doing it in enums
        if("C".equalsIgnoreCase(line)) {
            TempType t = TempType.valueOf(
        } else {
            System.err.println("Invalid temp type passed: one of 'c' or 'C' expected (without quotes)");
        }
    }

}

The advantage here is that for a big application (though your application isn't ATM, but we are here to learn, right?) all usages of this TempType won't be burdened by checking for two different types (c and C) for something which is the same i.e. Celsius temperature type. This is a big win in terms of expressiveness and maintainability.

To move a step above, you can code the same logic in the enum type by extending it to handle creation of TempType from string "c", "C", "CELSIUS" etc. rather than having the logic in your driver (main method) code. But that would come later; make sure you understand what has been posted above along with realizing why having two celsius representation would be a bad idea. Ask back if you still have questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.