The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Search Elements in ArrayList
Discuss Search Elements in ArrayList in the Java Help forum on Dev Shed. Search Elements in ArrayList Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 5th, 2013, 10:28 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 12 m
Reputation Power: 0
|
|
|
Homework - Search Elements in ArrayList
Hello,
I am trying to write a method that will compare a String (from user input) to elements in an ArrayList.
OurExtendedWord(String word, int pos) {
this.word = word;
this.pos = pos; }
class OurList {
private ArrayList<OurExtendedWord> list;
OurList() {
list = new ArrayList<OurExtendedWord>(); }
/* This is a skeleton for the find() method */
String find(String word) {
// Needs to take user input and compare the word to the list
// If there is a match, return the word and the position
// If no match, return null
return null;
}
So far, I have:
String find(String word) {
// Get user input from Scanner
String searchFor = input.next();
// Loop through ArrayList to search for word
for (OurExtendedWord extWord : list) {
if (searchFor.equals(extWord)) {
return extWord.getWord();
}
else {
return null;
}
}
}
Any ideas on how I can fix this code?
|

February 5th, 2013, 10:36 AM
|
 |
Contributing User
|
|
Join Date: May 2004
Location: Superior, CO, USA
|
|
|
Perhaps you should wait until you've walked through the entire list before returning null. You'll only compare with the very first element in the ArrayList with your current code.
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
|

February 5th, 2013, 02:33 PM
|
|
|
|
Can you explain what the meaning is of the attribute 'pos' in the object 'OurExtendedWord'? I have an idea what you want to achieve with it, but I'd like to hear it from you because you might want to rethink this design.
EDIT:
Repost of http://www.dreamincode.net/forums/topic/311227-search-for-elements-in-arraylist-given-user-input/
Last edited by Aurum84 : February 5th, 2013 at 02:37 PM.
|

February 6th, 2013, 06:46 AM
|
 |
Daniel Schildsky
|
|
Join Date: Mar 2004
Location: KL, Malaysia.
|
|
|
Problem with the code
Java Code:
Original
- Java Code |
|
|
|
for (OurExtendedWord extWord : list) { if (searchFor.equals(extWord)) { return extWord.getWord(); } else { return null; } }
This block of code got 2 problems:
a) one already mentioned by stdunbar,
b) You should not be comparing the string searchWord with your extWord, as extWord is an instance of OurExtendedWord instead of a String instance.
__________________
When the programming world turns decent, the real world will turn upside down.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|