user3044002
user3044002

Reputation: 59

Java - Trying to print an index in a String Array Using get method

So i have a String array

private  String[] transmission = {"Drive", "Park", "Reverse"};

Here is my set method, I'm practicing my switch statement as I don't use it often.

public void setTransmission(String[] transmission) {
    System.out.println("To change the transmission, enter D for Drive,P for Park or R for reverse");
    switch (input.nextLine()) {
        case "D":
        case "d":
            System.out.println("The Car is currently in Drive.");
            transmission[0] = this.transmission[0];
            break;
        case "P":
        case "p":
            System.out.println("The Car is currently in Park.");
            transmission[1] = this.transmission[1];
            break;
        case "R":
        case "r":
            System.out.println("The Car is currently in Reverse");
            transmission[2] = this.transmission[2];
            break;

    } 

Here is the real PROBLEM. In my getMethod it only prints the first index in the array:

 public String getTransmission()
    {
        return String.format("The car is currently in %s",transmission);
    }

How can i get it to print what the user entered? I know i could just use a string variable but i would prefer to use an array.

Upvotes: 0

Views: 605

Answers (1)

Max Fichtelmann
Max Fichtelmann

Reputation: 3504

assuming this occurs in the same class, you would need the index as an internal state.

a possible implementation would be:

public class Car
{
    private final String[] transmission = { "Drive", "Park", "Reverse" };

    private final Scanner  input;

    private int            index;

    public Car( Scanner input )
    {
        this.input = input;
    }

    public void setTransmission()
    {
        System.out.println( "To change the transmission, enter D for Drive,P for Park or R for reverse" );
        switch ( input.nextLine() )
        {
        case "D":
        case "d":
            System.out.println( "The Car is currently in Drive." );
            index = 0;
            break;
        case "P":
        case "p":
            System.out.println( "The Car is currently in Park." );
            index = 1;
            break;
        case "R":
        case "r":
            System.out.println( "The Car is currently in Reverse" );
            index = 2;
            break;
        }
    }

    public String getTransmission()
    {
        return String.format( "The car is currently in %s", transmission[index] );
    }
}

It would help, if you could could add a little more context to your problem. Especially how you call your methods and where they reside.

A better design would probably be to create an enum for the transmission and separate the parsing from the data.

This would reduce the car to a data container without any logic:

public class Car
{
    public enum Transmission
    {
        Drive, Park, Reverse
    }

    private Transmission transmission;

    public void setTransmission( Transmission transmission )
    {
        this.transmission = transmission;
    }

    public String getTransmission()
    {
        return String.format( "The car is currently in %s", transmission );
    }
}

An application, that uses this Car class would the parse the transmission and set the typed Transmission in the car:

public class CarApplication
{
    public static void main( String[] args )
    {
        try ( Scanner input = new Scanner( System.in ) )
        {
            System.out.println( "To change the transmission, enter D for Drive,P for Park or R for reverse" );
            String answer = input.nextLine();
            Transmission transmission = parseTransmission( answer );

            Car car = new Car();

            car.setTransmission( transmission );

            System.out.println( car.getTransmission() );
        }
    }

    private static Transmission parseTransmission( String input )
    {
        switch ( input )
        {
        case "D":
        case "d":
            return Transmission.Drive;
        case "P":
        case "p":
            return Transmission.Park;
        case "R":
        case "r":
            return Transmission.Reverse;
        default:
            throw new IllegalArgumentException( "expected an input D,P,R but got: " + input );
        }
    }
}

Upvotes: 1

Related Questions