3

I stuck with the logic and need help for my project.
I have a text file with 5 vehicle names in it.
Then I have to read them into an ArrayList, which I did using:

public static void main(String[] args) throws IOException
{
 BufferedReader bTextFileVehicleNames = new BufferedReader(new FileReader("vehicles.txt"));
 ArrayList<String> vechicleNamesArray = new ArrayList<String>();
 while((textFileLine = bTextFileVehicleNames.readLine()) != null)
 { 
 vehicleNamesArray.add(textFileLine);
 }

Now, I need to create an object for every item (vehicle name) in the ArrayList, which I did using:

for(String s : vehicleNamesArray)
{
 newVehicle = new Vehicle(s);
 //I defined this Vehicle newVehicle = null; in the main class. My main is also in the same Vehicle.java file.
}

Each of these objects have attributes: power, angle, speed; and behaviours: powerOn(), powerOff(), SpeedUp(), SlowDown(), etc.. which I defined in Vehicle class.

Now, the user has to enter Vehicle name command. Then I split them and took [0], which is the vehicle name, and have to first check if it is one of those vehicle names in the ArrayList (derived from txt file), and if it is then have to refer it to that particular object I created above. And then have to take in the [1], which is command that corresponds to the particular behaviour of object, which can be powerOn, speedUp, etc etc..

for (int i=0; i< vehicleNamesArray.size(); i++)
{
 if (vehicleNamesArray.get(i).matches(userVehicleName))
 //userVehicleName is the [0] vehicle name entered by user
 //userVehicleCommand is the [1] command entered by user
 {
 switch (userVehicleCommand.toLowerCase()) 
 {
 case "power on":
 newVehicle.powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
 System.out.println(newVehicle.getName()+" is powered ON.");
 break;
 ...
 ...

For example, let's consider there are 5 vehicles in the txt (thereby in the ArrayList) named: Audi, Bmw, Mercedez, Volkswagen, Porsche. The problem is that whatever I enter in the vehicle name command, the program by default is taking the last element, in this case Porsche in the ArrayList. I.e., when I say "Audi, powerON", it still prints out "Porsche is powered ON."

I think I messed up in linking the user entered name to check with the ArrayList and then refering it to that particular object.

Samurai
3,7295 gold badges29 silver badges41 bronze badges
asked May 28, 2015 at 19:43
9
  • 3
    Yes, you have one newVehicle object and you assign each item to it. At the end, the last assignment is what's left. Also, you should use a try-with-resources block to close your BufferedReader. Commented May 28, 2015 at 19:47
  • 1
    Does your user uses regex? if no in first 'if' statement use .equals(), and in my expirience switch function works kinda bad with strings, consider using if statements there too Commented May 28, 2015 at 19:47
  • Your newVehicle variable is one variable holding one vehicle instance, thus if you assign it a value in a loop, it will retain the last one. If you want to have a list of vehicles, well, you have to create and populate a list. Commented May 28, 2015 at 19:47
  • In the switch block instead of newVehicle.getName() you have to use vehicleNamesArray.get(i).getName() or userVehicleName Commented May 28, 2015 at 19:48
  • @Titus where did you get that idea from? Commented May 28, 2015 at 19:48

2 Answers 2

1

Keep objects created in List also :

List<Vehicle> vehicleList = new ArrayList<>;
for(String s : vehicleNamesArray)
{
 newVehicle = new Vehicle(s);
 vehicleList.add(newVehicle);
 //I defined this Vehicle newVehicle = null; in the main class. My main is also in the same Vehicle.java file.
} 

Then in your second for loop retrieve that vehicle object and update it:

for (int i=0; i< vehicleNamesArray.size(); i++)
{
 if (vehicleNamesArray.get(i).matches(userVehicleName))
 //userVehicleName is the [0] vehicle name entered by user
 //userVehicleCommand is the [1] command entered by user
 {
 switch (userVehicleCommand.toLowerCase()) 
 {
 case "power on":
 vehicleList.get(i).powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
 System.out.println(vehicleList.get(i).getName()+" is powered ON.");
 break;
 ...
 ...
answered May 28, 2015 at 20:01
Sign up to request clarification or add additional context in comments.

Comments

0

You might want to try storing the vehicles in a HashMap or something:

HashMap<String, Vehicle> vehicles = new HashMap<String, Vehicles>();
for(String s : vehicleNamesArray)
{
 vehicles.put(s, new Vehicle(s));
}
//Access the vehicle from user input
Vehicle currentVehicle = vehicles.get(userVehicleName);
switch (userVehicleCommand.toLowerCase()) 
{
 case "power on":
 currentVehicle.powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
 System.out.println(currentVehicle.getName()+" is powered ON.");
 break;
 //etc...
}

A HashMap can be used to store key,value pair in your case key=carname value=Vehicle object. This will make it easy to go back and select the car you want based on the user input. Also currently your not storing your Vehicle objects you are only storing the last one...

answered May 28, 2015 at 19:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.