0

I'm learning Java and I'm creating a small program with several classes that inherit from another. Where basically the program is about a dealership that has several vehicles for sale both motorcycles and cars; and then you as a user can register and such. The question is that I have created a function that receives an array of string type of 2 values, where each of those values is then used to return an Object of type car or motorcycle depending on the content of that array. In the beginning that function works perfectly, I pass an array with the correct strings, and it returns an object to me; but when I pass an array where its values were entered from a string that was requested from the user; it returns null, that is, it never returns any object. And the most curious thing is that I print on the screen this array, and it really fills those positions correctly.

In this way everything works correctly and I return the specs of the vehicle:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
        //I create an array of 2 positions where I enter those values
        String[] respuesta = new String[2];
        respuesta[0] = "moto";
        respuesta[1] = "deportiva";
        
        //I pass the array to the function as a parameter, and if the values of the array match, the function returns an object. and then I use that function to call a method of the class vehicle I've created.
        
        System.out.println(Concesionario(respuesta).SpecsVehiculo());




//this is the function that calls from the main that creates objects of type car or motorcycle depending on the values in positions 0 and 1 of the array passed by parameter.

    static Vehiculo Concesionario(String[] vehiculoModelo)
    {
        
        Moto scooter = new Moto();
        if(vehiculoModelo[0]== "moto")
        {
            switch(vehiculoModelo[1])
            {
                
                case "deportiva":
                    Moto deportiva = new Moto(vehiculoModelo[1], "600cc", 110, 8400);
                    return deportiva;
                case "turismo":
                    Moto turismo = new Moto(vehiculoModelo[1], "1200cc", 140, 18500);
                    return turismo;
                case "enduro":
                    Moto enduro = new Moto(vehiculoModelo[1], "250cc", 25, 5200);
                    return enduro;
                default:
                    return scooter;
            }
        }
        else if(vehiculoModelo[0]== "coche")
        {
            Coche sedan = new Coche();
            switch(vehiculoModelo[1])
            {
                
                case "deportivo":
                    Coche deportivo = new Coche(vehiculoModelo[1], "3.0 tfsi","Trasera","Gasolina",3, 350, "Secuencial 6 marchas", 65400);
                    return deportivo;
                case "offroad":
                    Coche offroad = new Coche(vehiculoModelo[1], "2.5 tdi","4x4","Diesel",5, 150, "Manual 5 marchas", 31700);
                    return offroad;
                case "familiar":
                    Coche familiar = new Coche(vehiculoModelo[1], "1.8 eco burst","4x4","Gasolina hibrido",5, 120, "Automatico", 28500);
                    return familiar;
                default:
                    return sedan;
            }
        } else return null;
                
    }

But when I fill the array in this way it returns null:

 public static void main(String[] args) {
            // TODO Auto-generated method stub

            Scanner scanner = new Scanner(System.in);
            
            String[] respuesta = new String[2];
            System.out.print("escribe moto o coche: ");

            respuesta[0] = scanner.nextLine();
            System.out.print("escribe el modelo: ");
            respuesta[1] = scanner.nextLine();
            
            
            
            System.out.println(Concesionario(respuesta).SpecsVehiculo());

0 Answers0