Results 1 to 4 of 4
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default String match to the linkedlist element

    Hi,

    I have created a LinkedList and I don't know why I can't check to see if the String elements entered into the list(Toyota) matches the input from the user, here is a piece of my code:

    public class Car
    { private LinkedList<Type> make = new LinkedList<Type>();
    private String car;
    public Car(String car)
    { this.car = car;
    add();
    check(); }

    private void add()
    {make.add(new Type("Toyota"));}

    private void check()
    {car = In.nextLine();
    if(isCar(car))
    car = In.nextLine();
    else{ System.out.print("\t\tThat is not a valid");
    System.out.print("\t\tEnter car: ");
    car = In.nextLine();}}}

    private boolean isCar(String car)
    { for (Type t: make)
    if (t.matches(car))
    return true;
    return false; }

    }

    This is the Type class:


    public class Type
    { private String type;

    public Type(String type)
    { this.type = type;
    }

    public boolean matches(String car)
    { return this.type.equals(car); }

    }
    Last edited by jboy; 09-06-2009 at 05:30 AM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Why do you read in the car 2-3 times? Do you get "That is not a valid" message? What is "In"? Does it compile?
    My Hobby Project: LegacyClone

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Ok, thanks, I found out the I had an extra read in method which wasn't necessary. So I got it solved. Thanks anyway..
    Last edited by jboy; 09-06-2009 at 06:53 AM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Add System.out.println statements to your code to let you see the values of critical variables before critical statements. Also please remember to format your code and post it using code tags next time you post code.

Similar Threads

  1. Need help getting java drawing match to image.
    By kiduut in forum New To Java
    Replies: 9
    Last Post: 12-28-2008, 01:55 PM
  2. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  3. Java Developer - Need Job - This is Perfect match
    By ravisrivastava2003 in forum Java Software
    Replies: 0
    Last Post: 12-10-2008, 07:35 PM
  4. Replies: 0
    Last Post: 07-07-2008, 04:51 PM
  5. Get all groups from a regular expression match?
    By johann_p in forum New To Java
    Replies: 0
    Last Post: 05-16-2008, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •