Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Finding String in ArrayList

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finding String in ArrayList

    I am having some trouble searching an ArrayList to see if it contains a string.

    public Book findByTitle(String title){
     
    	    Book book = new Book();
     
    	    int location;
    		if(books.contains(title)){
    		 location = books.indexOf(title);
    		 book = books.get(location);
    		 		}
    		else{
    			return null;
    		}
    		    return book;
    			}

    I am told to search for a book by its title, if it is found return the Book otherwise return null. This method is called from

       private void findBook(Library lib) {
          Scanner scn = new Scanner(System.in);
          String title;
          Book book;
     
          System.out.print("Title: ");
          title = scn.nextLine();
     
          book = lib.findByTitle(title);
     
          System.out.println("ISBN: " + book.getIsbn());
          System.out.println("Title: " + book.getTitle());
          System.out.println("Author: " + book.getAuthor());
          System.out.println("RRP: " + book.getRrp());
       }

    However I get a null pointer exception

    Thank you for any help


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Finding String in ArrayList

    books.contains(title)

    'title' is a String object. Look at the API doc for ArrayList.contains(Object) - it explains why your code isn't working.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding String in ArrayList

    I assume and Object is something that would be in another Array? such as Book is an object?

    What do I use to see if the array contains a String then?

    Thank you for your help

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Finding String in ArrayList

    Did you read the explanation at java.util.ArrayList.contains(Object)? Book and String are both subclasses of Object, but when you use ArrayList.contains(Object) on an ArrayList filled with Book objects, you need to specify an object argument for whom the equals() method might return true. aBookObject.equals(yourStringObject) is not likely to return true for any of the Book objects in your ArrayList.

    edit:
    What do I use to see if the array contains a String then?
    You would have to iterate over all the objects in the ArrayList, comparing your search String with an appropriate String obtained from each object. The .contains method, if Book.equals() is implemented in any sane way would almost certainly be doing exactly that. I personally wouldn't use ArrayList.contains() for this particular task, I would iterate over the objects and do the string comparison. Using something like a HashMap would greatly simplify code for exact-match search.
    Last edited by Sean4u; August 6th, 2011 at 05:23 AM.

Similar Threads

  1. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  2. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 07:57 PM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 08:58 AM
  4. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  5. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 01:16 PM