Java - Trying to print an index in a String Array Using get method -
Then I have a string ARA
personal string [] transmission = {"drive "," Park "," reverse "};
This is my set method, I am practicing my switch statement because I do not use it often.
Public Zero Set Transmission (string [] transmission) {System.out.println ("To change 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 the drive."); Transmission [0] = this Transmission [0]; break; Case "P": Case "P": System.out.printLN ("The car is currently in the 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; }
The actual problem here is in my getMethod it prints the first index in the array only:
public string getTransmission () {return string. Format ("the car is currently in% s, the transmission);}
How can I print what the user has entered? I know that I only have a string variable But I would prefer to use an array.
Believing it is In the same class, you will need the index as an internal situation.
The possible implementation will be:
public Nick class car {private finals string [] transmission = {"drive", "park", "reverse"}; personal finals scanner input; private index index; public car (scanner input) {this.input = input;} public Zero set transmission () {System.out.println ("To change transmission, enter D for drive, P for park or for reverse"); switch (input.nextLine ()) {case "D" : Case "d": System.out.println ("The car is currently in the drive."); Index = 0; The rupture; The case "P": The case " Mr. "Sistmkautkprintelan (" the car park at present. "); 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 ("car currently is% s", transmission [index]); }}
This will help you, if you can add a little more context to your problem, especially where you live and where you live.
A better design possibly to create an enum for transmission and to separate the parsing from the data.
This will reduce the data container without any logic:
public class car {public anonym transmission {drive, park, reverse} private transmission transmission; Public Zero Set Transmission (transmission transmission) {this.transmission = transmission; } Public string getTransmission () {return string. Format ("The car is currently in% s, the transmission);}}
An episode, which uses this car class, will set the parse transmission and type transmission in the car: / P>
Public class CarApplication {public static zero 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 reply = Input.Nextine (transmission) transmission transmission = expression (north); car car = new car; car.settransmission (transmission); system.out.println (car.getTransmission ());}} private static transmission pars transmissions (String input) {switch (input) {case "D": case "D": return transmission. Drive; case "P": case "P": return transmission. Case "R": Case "R": Return Transmission Default: Throw an exception to the new illegal archive ("Input D, P, R I was expecting, but click on: "+ input";}}}
Comments
Post a Comment