• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Search for an object in an arrayList

Hello Experts,

Hopefully you can help me because I can't get it right. I'm trying to search in the arrayList for an object of the type numbersWord an then invoke a method from another class.

When the user enters the word (option D), it has to locate the word entered by the user in the corresponding  numbersWord object within the ArrayList.

This is what I have so far but it's not working.

import java.util.ArrayList;
import java.util.Scanner;

public class enterNumbersWord {
	
	
	public static ArrayList<numbersWord> nw = new ArrayList<numbersWord> ();
	
	
	public static void main (String[] args){
		
		Scanner input = new Scanner(System.in);
	
	String answer;
	int numa, numb, numc;
	String wd;
	String searchWord;

	do
	{
		System.out.println("A - Enter numbers and words");
		System.out.println("B - Enter more numbers and words");
		System.out.println("C - Print Details");
		System.out.println("D - Search");
		System.out.println("X - Exit");
		System.out.print("");
		
		System.out.print("Enter your selection: ");
		answer = input.next();
		System.out.print("");
		
		
	if ("A".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter a word: ");
		wd = input.next();
		System.out.print("");
		
		numbersWord z = new numbersWord(numa, numb, numc, wd);
		nw.add(z);
		
	}
	
	if ("B".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter your word: ");
		numa = input.nextInt();
		
		moreNumbers y = new moreNumbers(numa, numb, numc, wd);
		nw.add(y);
		
	}
	
	else if ("C".equalsIgnoreCase(answer)){
		
		System.out.print("Print Details: ");
		System.out.println("");
		
		for (numbersWord x: nw){
			x.print();	
			}
		}
	
	else if ("D".equalsIgnoreCase(answer)){
		
		System.out.print("Enter Word:");
		searchWord = input.nextLine();

		
		//********************************************************************
		// search in the array for the word of the object type numbersWord
		
		boolean found = false;
		for(int i=-1;i++<nw.length-1){
			
			if(nw[i].getWord().equalsIgnoreCase(searchWord)){
					found = false;
							
			System.out.println("Word Not Found");
			}	
			else{
				
				//************************************************************
				// calling the method lessNumbers
				
				numbersWord method = new numbersWord (numa, numb, numc, wd);
				method.lessNumbers ();
				
				if (method.lessNumbers () == true){
					
					System.out.println("Success");
					
				}
				else{
					System.out.println("Failure");
					
				}
				
			}
		} //while
		
	} //elseif
	
	else if ("X".equalsIgnoreCase(answer)){
		break;
		
		
	}
	else{
		
		System.out.println("Error, please try again");
		System.out.println("");
		
	}
	
		}while(!(answer.equals("X")));

	}

} 

Open in new window


public class numbersWord {

	
	private int number1;
	private int number2;
	private int number3;
	private String word;
	
	public numbersWord (int number1, int number2, int number3, String word){
		
		this.number1 = number1;
		this.number2 = number2;
		this.number3 = number3;
		this.word = word;
	}
	
	
	public int getNumber1 (){
		return number1;
	}
	
	public int getNumber2 (){
		return number2;
	}
	
	public int getNumber3 (){
		return number3;
	}
	
	public String getWord (){
		return word;
	}
	
public boolean lessNumbers (){
		
		if (number1 < number2 ) {
		    return true;
		  }
		else{
			return false;
		}
		
	}
	

		public void print () {
			
		System.out.println("Number 1 is : " + number1);
		System.out.println("Number 2 is : " + number2);
		System.out.println("Number 3: " + number3);
		System.out.println("Your word is: " + word);
		
	}
	
}

Open in new window

0
caterco
Asked:
caterco
  • 5
  • 4
  • 4
2 Solutions
 
krakatoaCommented:
ArrayList.contains(Object o) returns a boolean.
0
 
CEHJCommented:
This is what I have so far but it's not working.
http://technojeeves.com/joomla/index.php/free/116-doesntwork
0
 
krakatoaCommented:
You've still got to sort out what object you are referring to by "moreNumbers" - it's not resolved at all, but meanwhile this fixes some other issues :

import java.util.ArrayList;
import java.util.Scanner;

public class enterNumbersWord {
	
	
	public static ArrayList<numbersWord> nw = new ArrayList<numbersWord> ();
	
	
	public static void main (String[] args){
		
		Scanner input = new Scanner(System.in);
	
	String answer;
	int numa, numb, numc;
	String wd;
	String searchWord;

	do
	{
		System.out.println("A - Enter numbers and words");
		System.out.println("B - Enter more numbers and words");
		System.out.println("C - Print Details");
		System.out.println("D - Search");
		System.out.println("X - Exit");
		System.out.print("");
		
		System.out.print("Enter your selection: ");
		answer = input.next();
		System.out.print("");
		
		
	if ("A".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter a word: ");
		wd = input.next();
		System.out.print("");
		
		numbersWord z = new numbersWord(numa, numb, numc, wd);
		nw.add(z);
		
	}
	
	if ("B".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter your word: ");
		numa = input.nextInt();
		
		moreNumbers y = new moreNumbers(numa, numb, numc, wd);
		nw.add(y);
		
	}
	
	else if ("C".equalsIgnoreCase(answer)){
		
		System.out.print("Print Details: ");
		System.out.println("");
		
		for (numbersWord x: nw){
			x.print();	
			}
		}
	
	else if ("D".equalsIgnoreCase(answer)){
		
		System.out.print("Enter Word:");
		searchWord = input.nextLine();

		
		//********************************************************************
		// search in the array for the word of the object type numbersWord
		
		boolean found = false;
		
		for(int i=-1;i++<nw.size()-1;){
		
			if(((numbersWord[])nw.toArray())[i].getWord().equalsIgnoreCase(searchWord)){
					found = false;
							
			System.out.println("Word Not Found");
			}	
			else{
				
				//************************************************************
				// calling the method lessNumbers
				
				numbersWord method = new numbersWord (numa, numb, numc, wd);
				method.lessNumbers ();
				
				if (method.lessNumbers () == true){
					
					System.out.println("Success");
					
				}
				else{
					System.out.println("Failure");
					
				}
				
			}
		} //while
		
	} //elseif
	
	else if ("X".equalsIgnoreCase(answer)){
		break;
		
		
	}
	else{
		
		System.out.println("Error, please try again");
		System.out.println("");
		
	}
	
		}while(!(answer.equals("X")));

	}

//} 
/////////////////////////////////////////////


public static class numbersWord {

	
	private int number1;
	private int number2;
	private int number3;
	private String word;
	
	public numbersWord (int number1, int number2, int number3, String word){
		
		this.number1 = number1;
		this.number2 = number2;
		this.number3 = number3;
		this.word = word;
	}
	
	
	public int getNumber1 (){
		return number1;
	}
	
	public int getNumber2 (){
		return number2;
	}
	
	public int getNumber3 (){
		return number3;
	}
	
	public String getWord (){
		return word;
	}
	
public boolean lessNumbers (){
		
		if (number1 < number2 ) {
		    return true;
		  }
		else{
			return false;
		}
		
	}
	

		public void print () {
			
		System.out.println("Number 1 is : " + number1);
		System.out.println("Number 2 is : " + number2);
		System.out.println("Number 3: " + number3);
		System.out.println("Your word is: " + word);
		
	}
	
}
}

Open in new window


Yes, I put the numbersWord as an inner class, but this is just to make life a bit easier for the discussion.

CEHJ's link should be checked out to do things properly.

(BTW your getWord search looks strange to me. Shouldn't you be delivering a positive boolean if the two match, and not false? Of course, I don't know if that *is* what you need, but . . .)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
catercoAuthor Commented:
Yes, I already checked CEHJ's link but believe me, before I posted a question I tried to search in the web, forums, etc and I tried using ArrayList.contains(Object o) but it doesn't work either or I don't know how to code it properly.

Krakatoa, I tried your code but is giving me this error:

Enter Word:Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [numbersWord;
	at .enterNumbersWord.main(enterNumbersWord.java:89)

Open in new window


I'm still a newby in java and I have no idea how to referred only to the object "moreNumbers" and how to fix the error, the search code works if is outside the do while loop.  I need some help please.
0
 
catercoAuthor Commented:
I'm trying to use ArrayList.contains but it's always given me an answer false

else if ("D".equalsIgnoreCase(answer)){
            
            System.out.print("Enter Word:");
            searchWord = input.next();
            
            boolean wordFound = nw.contains(searchWord);
            
            System.out.println("Is it in the array?" + wordFound);

            }
}
0
 
krakatoaCommented:
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;

public class enterNumbersWord {
	
	
	public static ArrayList<numbersWord> nw = new ArrayList<numbersWord> ();
	
	
	public static void main (String[] args){
		
		Scanner input = new Scanner(System.in);
	
	String answer="";
	int numa=0, numb=0, numc=0;
	String wd="";
	String searchWord="";

	do
	{
		System.out.println("A - Enter numbers and words");
		System.out.println("B - Enter more numbers and words");
		System.out.println("C - Print Details");
		System.out.println("D - Search");
		System.out.println("X - Exit");
		System.out.print("");
		
		System.out.print("Enter your selection: ");
		answer = input.next();
		System.out.print("");
		
		
	if ("A".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter a word: ");
		wd = input.next();
		System.out.print("");
		
		numbersWord z = new numbersWord(numa, numb, numc, wd);
		nw.add(z);
		
	}
	
	if ("B".equalsIgnoreCase(answer)){
		System.out.print("Enter the first number: ");
		numa = input.nextInt();
		System.out.print("Enter the second number: ");
		numb = input.nextInt();
		System.out.print("Enter the third number: ");
		numc = input.nextInt();
		System.out.print("Enter your word: ");
		numa = input.nextInt();
		
		//moreNumbers y = new moreNumbers(numa, numb, numc, wd);
		//nw.add(y);
		
	}
	
	else if ("C".equalsIgnoreCase(answer)){
		
		System.out.print("Print Details: ");
		System.out.println("");
		
		for (numbersWord x: nw){
			x.print();	
			}
		}
	
	else if ("D".equalsIgnoreCase(answer)){
		
		System.out.print("Enter Word:");
		searchWord = input.nextLine();

		
		//********************************************************************
		// search in the array for the word of the object type numbersWord
		
		boolean found = false;

		List<numbersWord> L = new ArrayList<numbersWord>((ArrayList<numbersWord>)nw);
		
		
		for(int i=-1;i++<nw.size()-1;){
		
			//if(((numbersWord[])nw.toArray())[i].getWord().equalsIgnoreCase(searchWord)){
			numbersWord n =(numbersWord)(L.toArray())[i];
			if((n.getWord().equalsIgnoreCase(searchWord))){
					found = false;
							
			System.out.println("Word Not Found");
			}	
			else{
				
				//************************************************************
				// calling the method lessNumbers
				
				numbersWord method = new numbersWord (numa, numb, numc, wd);
				method.lessNumbers ();
				
				if (method.lessNumbers () == true){
					
					System.out.println("Success");
					
				}
				else{
					System.out.println("Failure");
					
				}
				
			}
		} //while
		
	} //elseif
	
	else if ("X".equalsIgnoreCase(answer)){
		break;
		
		
	}
	else{
		
		System.out.println("Error, please try again");
		System.out.println("");
		
	}
	
		}while(!(answer.equalsIgnoreCase("X")));

	}

//} 
/////////////////////////////////////////////


public static class numbersWord {

	
	private int number1;
	private int number2;
	private int number3;
	private String word;
	
	public numbersWord (int number1, int number2, int number3, String word){
		
		this.number1 = number1;
		this.number2 = number2;
		this.number3 = number3;
		this.word = word;
	}
	
	
	public int getNumber1 (){
		return number1;
	}
	
	public int getNumber2 (){
		return number2;
	}
	
	public int getNumber3 (){
		return number3;
	}
	
	public String getWord (){
		return word;
	}
	
public boolean lessNumbers (){
		
		if (number1 < number2 ) {
		    return true;
		  }
		else{
			return false;
		}
		
	}
	

		public void print () {
			
		System.out.println("Number 1 is : " + number1);
		System.out.println("Number 2 is : " + number2);
		System.out.println("Number 3: " + number3);
		System.out.println("Your word is: " + word);
		
	}
	
}
}

Open in new window

0
 
CEHJCommented:
 boolean wordFound = nw.contains(searchWord);

Open in new window

That will never be true - you're comparing a String with a numberWord. That's an apples/oranges comparison. You need to call getWord on the numberWord in the List and compare that

for(numberWord nword : nw) {
    found = nword.getWord().equals(searchWord);
    if (found) {
       break;
    }
}

Open in new window

0
 
catercoAuthor Commented:
Thanks to both, I have been hours trying to figurate out.

Krakatoa for some reason I get errors with the code you provide me.

CEHJ, thanks so much the for loop and the explanation, it works perfect!!

But how would I do  it if it should only search within the object numbersWord?
0
 
krakatoaCommented:
Krakatoa for some reason I get errors with the code you provide me.

That's because all I've done is to get it to compile. You can run the code, and then fix the exceptions, but with the errors it wouldn't even get that far.
0
 
CEHJCommented:
But how would I do  it if it should only search within the object numbersWord?

I don't understand the question. I though the objective was to search the List for a certain numbersWord ..?
0
 
catercoAuthor Commented:
Sorry I did not explain properly, yes that's the objective but it should only look in the array for the details entered by the user of the type numbersWord and right now it's searching for the numbersWord and the moreNumbers.  Hopefully you understand what I'm trying to explain.
0
 
CEHJCommented:
Hopefully you understand what I'm trying to explain.
I'm afraid not ;) numbersWord has 4 attributes. At the moment, you're using just one of those (the word) as the search criterion. You can of course use any one or a combination of those attributes
0
 
catercoAuthor Commented:
No problem CEHJ, thanks a lot for your help. I will post another question more specific as I need to check in if what the user has entered is of a specific object type.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now