Solved

Search for an object in an arrayList

Posted on 2013-05-09
13
307 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

920 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

13 Experts available now in Live!

Get 1:1 Help Now