
I have an input of a string and a Car class. The string is the name of the car, which i have multiple in my linked list. I need to be able to go through my linked list of cars and determine which car to call by using the input string. I'm not sure if there's a .equal method to do this for i've tried the traditional way.

Here is a code where we have created a class Car. Then we have created another class in which we have stored the car object into linkedlist with some values. The code will then accept the car name from the user and display the price along with the car name.
import java.util.*;
class Car {
private String name;
private int price;
public Car(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
public class CarsList {
public static void main(String args[]) {
LinkedList<Car> carlist = new LinkedList<Car>();
carlist.add(new Car("Tata Nano", 100440));
carlist.add(new Car("Alto", 234230));
carlist.add(new Car("Swift", 466450));
carlist.add(new Car("Fusion" , 465656));
carlist.add(new Car("IKon", 664460));
carlist.add(new Car("Scorpio",705775));
Scanner input = new Scanner(System.in);
System.out.print("Enter car name: ");
String carname = input.nextLine();
for(Car s : carlist){
if (carname.equals(s.getName())) {
System.out.println(s.getName() + "=> " + s.getPrice());
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.