0

public int payment(){

        int Carpayment;
        int hour;
        String type;
        
        if (type == "MyVi")
            Carpayment = 10 * hour;
        else 
            if (type == "Iswara")
                Carpayment = 15 * hour;
        else 
            if (type == "Waja")
                Carpayment = 20 * hour;
        else 
            if (type == "Vios")
                Carpayment = 25 * hour;
        else 
            if (type == "Civic")
                Carpayment = 30 * hour; 
        
        return Carpayment;
     }

when i run this code.. error Carpayment might have not been initialized

SCHIER
  • 1
  • 2
  • 4
    You should **not** compare `String`s with `==`, use `equals` for that. [See here](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – MC Emperor May 18 '21 at 08:39

2 Answers2

1

Some Rectifications you can do

  1. Comparing type == "MyVi" , you should not compare String's with == operator because it will compare for object i.e hashcode comparison. You should use type.equals("MyVi");

  2. You should not use local variables to calculate the car payment as they need to be initialized so you should pass them to the payment method or use them as an instance variable.

    public int payment(int Carpayment,int hour,String type){
    
        //int Carpayment;
        //int hour;
        //String type;
    
        if (type == "MyVi")
            Carpayment = 10 * hour;
        else 
            if (type == "Iswara")
                Carpayment = 15 * hour;
        else 
            if (type == "Waja")
                Carpayment = 20 * hour;
        else 
            if (type == "Vios")
                Carpayment = 25 * hour;
        else 
            if (type == "Civic")
                Carpayment = 30 * hour; 
    
        return Carpayment;
     }
    

3.You can do something like this

    Class Car {
       int Carpayment;
       int hour;
       String type;
       Car(int hour,String type) {
       this.hour = hour;
       this.type = type;
       }
       public int payment(){

            if (type == "MyVi")
                Carpayment = 10 * hour;
            else 
                if (type == "Iswara")
                    Carpayment = 15 * hour;
            else 
                if (type == "Waja")
                    Carpayment = 20 * hour;
            else 
                if (type == "Vios")
                    Carpayment = 25 * hour;
            else 
                if (type == "Civic")
                    Carpayment = 30 * hour; 

            return Carpayment;
         }
       }
       Car civic_1 = new Car(4,"Civic");
       System.out.println(civic_1.payment());//Prints the Car payment to be done
Ashish Mishra
  • 704
  • 1
  • 6
  • 20
-1

It is occurs because you do not have a last else clause. So than, you can pass for all ifs and do not enter and anyone. To solve this problem you can:

  1. Initialise the Carpayment variable with a default value (better option in my opinion);

    public int  payment(){
     int Carpayment;
             int hour;
             String type;
    
     if (type == "MyVi")
         Carpayment = 10 * hour;
     else 
         if (type == "Iswara")
             Carpayment = 15 * hour;
     else 
         if (type == "Waja")
             Carpayment = 20 * hour;
     else 
         if (type == "Vios")
             Carpayment = 25 * hour;
     else 
         if (type == "Civic")
             Carpayment = 30 * hour; 
    
     return Carpayment;
    }
    

or

  1. Put a last else clause in the last else if.

    public int  payment(){
     int Carpayment;
             int hour;
             String type;
    
     if (type == "MyVi")
         Carpayment = 10 * hour;
     else 
         if (type == "Iswara")
             Carpayment = 15 * hour;
     else 
         if (type == "Waja")
             Carpayment = 20 * hour;
     else 
         if (type == "Vios")
             Carpayment = 25 * hour;
     else 
         if (type == "Civic")
             Carpayment = 30 * hour;
     else 
       Carpayment = 0;
    
     return Carpayment;
    }