Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
4  
You should swap your assignments in your setTransmission method. I.e this.transmission[0] = transmission[0]; and so on. –  Alexis C. Feb 17 '14 at 20:11
    
Tried but no luck :( –  user3044002 Feb 17 '14 at 20:13
    
You could keep track of the index where the new transition was inserted and return transmission[index] at the getter. –  Smutje Feb 17 '14 at 20:14
2  
@DavidWallace Then I don't see what's the point of this getTransmission method. Should it return the content of the all array or not? –  Alexis C. Feb 17 '14 at 20:16
1  
You will need to have some kind of field in your class that stores the current "gear", so that it can be returned. You could either store it as the String value itself, or as an int indicating an index into the array. Assign this field within the setTransmission method. –  David Wallace Feb 17 '14 at 20:16

1 Answer 1

up vote 1 down vote accepted

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 );
        }
    }
}
share|improve this answer
    
Also please note, that switch for Strings was introduced only in Java 7. If you are using Java 6 and below, you have to use if statement –  nikis Feb 17 '14 at 20:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.