Determine if string is in class

Ads
 

Determine if string is in class

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.

View Answers

July 27, 2012 at 12:12 PM

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());
        }
    }
   } 
 }

Ads









Related Tutorials/Questions & Answers:
Determine if string is in class
Determine if string is in class  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.... import java.util.*; class Car { private String name; private int price
(ii) String and String Buffer Class
(ii) String and String Buffer Class  difference between String and String Buffer Class
Advertisements
(ii) String and String Buffer Class
String and String Buffer Class  differences between : String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  difference between String and String Buffer Class... between String and StringBuffer 1) String object is immutable
String Class implementation in API
String Class implementation in API  Hi all, I was confused ,when am looking into java String class single argument constructor into that we... we send String as argument for String class
String tokenizer class
String tokenizer class  Hai... What is string tokenizer class? & what's its purpose
String tokenizer class
String tokenizer class  Hai... What is string tokenizer class? & what's its purpose
String tokenizer class
String tokenizer class  Hai... What is string tokenizer class? & what's its purpose
how to use string of one class into another Class
how to use string of one class into another Class  I created textField(JTextField t1=new JTextField();(it is in ClassA. The input of this textField i.e t1.getText() is to be used in different(another Class say in ClassB. How can
c++ new, delete, string class
c++ new, delete, string class  Write one program containing all these string exercises. Read a string from the user into a string named string1... contained in string1: Exercise 1 Good morning Create a new string named string2
how to pass a string from one class to another
how to pass a string from one class to another  hi all, Good morning. I am trying to develop a coding pass a string from one class and declare in other class. the string which i need to pass is the data from an file and i want
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I... input. It has to have multiple classes so I will have a main class, and a class... In these classes I need to create string arrays that contain artist names, and be able
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I... input. It has to have multiple classes so I will have a main class, and a class... In these classes I need to create string arrays that contain artist names, and be able
Determining the Word Boundaries in a Unicode String
will learn how to determine the word boundaries in a unicode string. Generally, we use split() method and StringTokenizer class to break the string into words... java.text.*; public class WordBoundaries { public static void main(String
String End with Example
String End with Example       This section tells you, how to determine the given string ends with specified string. The following program helps you to check whether the given
Determining the Character Boundaries in a Unicode String
Determining the Character Boundaries in a Unicode String In this section,  you will learn how to determine the character boundaries in a string. By determining the character boundaries, you will be able to break the string
Determining the Sentence Boundaries in a Unicode String
Determining the Sentence Boundaries in a Unicode String In this section, you will learn how to determine the sentence boundaries in a unicode string. From the previous section, you all are aware of BreakIterator class. This class
How to pass Array of string from action class to jsp page
How to pass Array of string from action class to jsp page  this is my action class package login.ipm; import java.sql.*; import java.util.ArrayList... class DiagservicesAction extends org.apache.struts.action.Action { private static
string
string  a java program using string function to input any string and print the same in alphabetical order   import java.util.*; class ArrangeStringAlphabetically { public static void main(String[] args
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
String intern()
the intern() method of String class. We are going to use intern() method of String...; operator to determine which Strings are equal. Basically an intern string is the one.... The String class maintains a pool of strings privately, which is empty initially
string
*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str...string  a java program to input a string and display the string
string
; public class StringTest { public static void main(String [] args...string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C
string
and also displaying that contain word? Like I want to find "a" in the string... and a character. It then count the occurrence of that character in the string and display the words that contains that character. import java.util.*; public class
how to pass string array from action class to jsp page? pls reply me.
how to pass string array from action class to jsp page? pls reply me.  how to pass string array from action class to jsp page? pls reply me.  .... In your action class, String[] arr = {"A","B","C","D"}; for (int x = 0; x < arr
string
string  program for String reverse and replace in c,c++,java... code: import java.util.*; class ArrayReverse { public static void main(String[] args) { String array[]=new String[5]; Scanner input
String lastIndexOf(String str)
the lastIndexOf(String str) method of String class. We are going to use lastIndexOf(String str) method of String class in Java. The description of the code... String lastIndexOf(String str)     
String indexOf(String str)
the indexOf(String str) method of String class. We are going to use indexOf(String str) method of String class in Java. The description of the code is given below... String indexOf(String str)     
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
please clarify regarding capacity method of my below string buffer class example
please clarify regarding capacity method of my below string buffer class example  hi here is the program below: > import java.lang.*; public class > Compare{ public static void > main(String[] args) { >

Ads