Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Search for Elements in ArrayList, Given User Input

$
0
0
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;
}
}
}



However, I am running into problems, as it will not detect any matches. How can this be solved?

Viewing all articles
Browse latest Browse all 51036

Trending Articles