Solved

Search for an object in an arrayList

Posted on 2013-05-09
13
306 Views
Last Modified: 2013-05-12
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
Comment
Question by:caterco
  • 5
  • 4
  • 4
13 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39153831
ArrayList.contains(Object o) returns a boolean.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39154036
This is what I have so far but it's not working.
http://technojeeves.com/joomla/index.php/free/116-doesntwork
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39154126
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
 

Author Comment

by:caterco
ID: 39155965
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
 

Author Comment

by:caterco
ID: 39156081
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
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 200 total points
ID: 39156450
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 39156464
 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
 

Author Comment

by:caterco
ID: 39156839
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39156895
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39157123
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
 

Author Comment

by:caterco
ID: 39158506
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39158723
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
 

Author Comment

by:caterco
ID: 39159698
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now