gergomat
gergomat

Reputation: 57

How to initialize a string?

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 ?

Upvotes: 2

Views: 23621

Answers (5)

Peter_James
Peter_James

Reputation: 647

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;
        }
    }
}

Upvotes: 0

Peter_James
Peter_James

Reputation: 647

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 = "";
}

Upvotes: 0

gerosalesc
gerosalesc

Reputation: 3063

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

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

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.

Upvotes: 0

Makoto
Makoto

Reputation: 106390

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 = "";

Upvotes: 5

Related Questions