Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Determine if string is in class
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


BOB
Determine if string is in class
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners


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.


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


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
java class string - Java Beginners
java class string  Write a program that reads three strings; then appends to the first string the string formed when extracting from the second string all the characters until the first occurrence of the third string
Convert String to Class - JSP-Servlet
. Reading job name, class name, day & time using xml file only. Problem is while reading class name, retrieve as a string format but Quartz required in "Class" format...Convert String to Class  Hi all, I am using Quartz as a scheduler
String Class in Action Script3
.style1 { font-size: medium; } String Class in Action Script3:- String class is a data type. String class provides methods for access... types of String class objects. String class have different types of constructors
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
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 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
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
Date Class with Single String parameter constructor example
.style1 { font-size: medium; } Date class with single string parameters to the Date() constructor example:- The Date class have a single string parameterized constructor. If user pass a string parameter in the date class
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 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
String Compression using Deflater class in java
String Compression using Deflater class in java. In this tutorial, we will define the use of Deflater class. The Deflater class is used to compressed data by using ZLIB library. The Deflater class is available in java.util.zip package
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
ReadLine(String fmt,Object... args) of Console class - Java Beginners
ReadLine(String fmt,Object... args) of Console class  How to reload the string and objects of console class in Java?  Hi friend,import java.io.Console;import java.io.PrintWriter;import java.util.Scanner; public class
JDOM CDATA Example, Use of append(String str) method of CDATA class.
JDOM CDATA Example, Use of append(String str) method of CDATA class.... The CDATA class is availablein org.jdom package. The append( java.lang.String string...(java.lang.String string) constructor of CDATA class crates a CDATA new node
using session variable determine the no of pages viewd by the user
using session variable determine the no of pages viewd by the user  using session variable determine the no of pages viewd by the user   Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial
determine the top 10 highest and 10 lowest in java
determine the top 10 highest and 10 lowest in java  hello, I have a case, where there are tables in the database and have a file borrowing that contains the codebooks and namesmembers. I want to make 10 the highest rank anyone
C String length
C String length       In this section, you will learn how to determine the length of a String in C. You can see in the given example, we have declared a string and initialize
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
String  how to add spaces with string functions.?   Hi Friend, Try the following code: class AddSpacesToString { public static String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s
string
Friend, Either you can use split() method: import java.util.*; class ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String
Advertisements
 

 

 

DMCA.com