11 Replies - 2037 Views - Last Post: 05 November 2010 - 05:37 AM Rate Topic: -----

#1 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Search an arrayList containg objects

Posted 04 November 2010 - 08:34 AM

Hi, i have searched this forum but have not been able to implement anything that works, here is my idea of the code. I want to search the arraylist Movies for a string called searchTerm which the user enters earlier in the program.


for(int i = 0; i < Movies.size(); i++){
			
			if(Movies.get(i).equals(searchTerm)){
				
				System.out.println(Movies.get(i));
			}
			
		}


I dont get any errors just nothing.

Any ideas and thanks in advance

Movies is an array list to confirm

This post has been edited by benshelly: 04 November 2010 - 08:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Search an arrayList containg objects

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Search an arrayList containg objects

Posted 04 November 2010 - 09:02 AM

try ignoring the case, searchTerm maybe in the arraylist but not entered exactly the way it appears in the arraylist.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,110
  • Joined: 27-December 08

Re: Search an arrayList containg objects

Posted 04 November 2010 - 09:38 AM

Can you show us what you add to the List?
Was This Post Helpful? 0
  • +
  • -

#4 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:21 AM

the array list Movies objects contain a title,genre,director,actors and rating. For testing purposes i have been entering every value as the same thing e.g. '1' and setting searchTerm as '1' but with no luck, i also tried variations of using indexOf in my code but with no luck.

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#5 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:32 AM

I was going to suggest using the indexOf() function, until I read your last post. Lol.
Was This Post Helpful? 0
  • +
  • -

#6 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:36 AM

Your arraylist contains Strings or "Movie" objects? If the second, then you need to override the equals method, of course. And, if what you using for a "searchTerm" is a string, you should probably be defining your own method (rather than using equals).
Was This Post Helpful? 0
  • +
  • -

#7 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:40 AM

It contains objects, sorry im new to Java overriding the equals what does that mean?

cheers
Was This Post Helpful? 0
  • +
  • -

#8 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:42 AM

http://download.orac...I/override.html
Was This Post Helpful? 0
  • +
  • -

#9 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 03:45 AM

will take a look and let you know where I get, thank you very much for all your help
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,110
  • Joined: 27-December 08

Re: Search an arrayList containg objects

Posted 05 November 2010 - 04:20 AM

View Postbenshelly, on 05 November 2010 - 06:21 AM, said:

the array list Movies objects contain a title,genre,director,actors and rating. For testing purposes i have been entering every value as the same thing e.g. '1' and setting searchTerm as '1' but with no luck, i also tried variations of using indexOf in my code but with no luck.

Thanks for your help

I'd still like to see some code. In your post I quoted, you're using single-quotes, which are for Characters, not Strings. This makes me think that could be part of the problem.
Was This Post Helpful? 0
  • +
  • -

#11 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 05:14 AM

i was typing in just the value 1 for example or any value such as movie.

Below is my exact method
textScanner() just uses the std input of the built in scanner function, i created a method to save on typing.

	public static void searchMovie(){
		//search arraylist for value in an object java
		String searchTerm = null;
		
		System.out.println("Please enter the movie search term");
		searchTerm = textScanner();
		
			for(int i = 0; i < Movies.size(); i++){
				if(Movies.get(i).equals(searchTerm)){
					System.out.println(Movies.get(i));
					            }   
				        }
			}





My program is defintatly storing the values in the arrayList as i can save to file and view them, and go to the previous and next values which are printed in the console window.
Was This Post Helpful? 0
  • +
  • -

#12 benshelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 10

Re: Search an arrayList containg objects

Posted 05 November 2010 - 05:37 AM

Ok fixed it, i called my method which sets my title and checked it for that.

public static void searchMovie(){
		//search arraylist for value in an object java
		
		String searchTerm = "";
		
		System.out.println("Please enter the movie you would like to find: ");
		searchTerm = textScanner();
		

		for(int i = 0; i < Movies.size(); i++){
			
			if(Movies.get(i).getTitle().equals(searchTerm)){
				
				System.out.println(Movies.get(i));
			}
			
		}





Sorry if i wasted your time
Thanks for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1