2
String car; 

    if (type == 'E')
        {fee = rate*50;
         car = "Economy";}

    else if (type == 'M')
         {fee = rate*70;
         car = "Midsize";}


    else if (type == 'F')
        {fee = rate*100;
        car = "Fullsize";}

System.out.println("Car type is " + car);

This is the part of my program that i have problem with. I get 'local variable car may not have been initialized ' . What should i do to make 'car' initialized ?

2
  • 2
    What about if type is not equals to E, M or F ? Just initialize it with String car = ""; Commented Nov 17, 2013 at 22:59
  • This is a situation where you should really use an enum for your E, M, and F values; you can assign names, descriptions, rates, and so on to the enum values, and it makes your code cleaner, safer, and more maintainable. Commented Nov 17, 2013 at 23:07

5 Answers 5

5

Java can't guarantee that car will ever become initialized. You don't have a guaranteed case (else) in your if statement that assigns car.

Initialize it to some value, such as an empty string.

String car = "";
Sign up to request clarification or add additional context in comments.

1 Comment

Is it good practice to always initialize your string variables to empty instead of null?
0

In general, you set your String to null to solve the compilation error.

String car = null;

That is because Java is trying to protect you from doing something you don't expect. If all the if statements are not executed, the variable will not get assigned. To tell Java, that you know that this might happen, you just chose explicitly to set it to null.

Comments

0

Java can't tell what's going to happen with your variable reach print, as it's initially pointing nowhere and further initializations are enclosed in conditionals Set your car variable to a default value depending on logic: "" or null are good options but it's really up to you in the end

Comments

0

I would consider to do this another way:

switch (type) {
 case 'E':
    fee =rate* 50;
    car = "Economy";
    break;
 case 'M':
    fee =rate* 70;
    car = "Economy";
    break;
 case 'F':
    fee =rate*100;
    car = "Economy";
    break;
 default:
    fee = 0;
    car = "";
}

Comments

0

How I would of solved this problem using an inner Enum class.

public class EnumExample {
    public enum CarType {
        E( 50,"Economy" ),
        F( 70, "Midsize" ),
        M( 100, "Fullsize");
        // Add as many as you want

        private int cost;
        private String type;

        CarType(int theCost, String theType) {
            cost = theCost;
            type = theType;
        }

        public int getFee(int rate) {
            return rate*cost;
        }

        public String toString() {
            return type;
        }
    }
    public static void main( String[] args ) {
        String type = "E";
        int rate = 25;

        switch( CarType.valueOf( type ) ) {
            case E:
                System.out.println( "The Car Type is: " + CarType.E );
                System.out.println( "The fee is: " + CarType.E.getFee(rate) );
                break;
            case F:
                System.out.println( "The Car Type is: " + CarType.F );
                System.out.println( "The fee is: " + CarType.F.getFee(rate) );
                break;
            case M:
                // ETC
            default:
                // Use a switch/case so you can add 'no car type' e.g Fee = 0;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.