Cannot search ArrayList in Java

Posted 25 January 2013 - 01:43 PM

I have to create an ArrayList of students which will store their first and last name, a student number, a major, and their gpa. I have the program working but I have to be able to search the array for a student based off user input and this is what I can't figure out. In class StudentList under method findStudent() I am trying to search through an ArrayList of what (I believe) is type Student and I am trying to search with a string.

I have tried many things such as simply trying to search using contains with and without loops, unsuccessfully attempted to convert the list to string via toString() and a StringBuilder. No matter how I set it up, it either won't compile or if it does, not matter what it returns false.

I attempted to comment out all the code that didn't work and erased it when it got to long figuring I would get it eventually and now realized that was a mistake as I cannot share with you all the mistaken ways I have been unable to find something in an arraylist. If anyone could point in the right direction I would greatly appreciate it.

Replies To: Cannot search ArrayList in Java

Re: Cannot search ArrayList in Java

Posted 25 January 2013 - 02:03 PM

You seem to have an ArrayList of student objects, but you're comparing the individual elements to strings, doubles, and ints.

You'll have to loop through the list of students, extract the appropriate field from each student object, and then compare on that. If you convert an object without an overridden ToString method, you'll get a memory address or something like that.

The user is asked to enter a search term. I assume that means the user can input anything, a String for either the first name, last name, or major, an int for the student number, or a double for the gpa. Is that right?

However you decide to accept the search term, you then search the Student objects in the list for the term.
So you need Student methods to locate the right Student. Let's say you know you're looking for the student object's first name, "Steve". You'd create a Student method:

public String getFirst()
{
return first;
}

Then you could check each Student object in the list:

if ( list.get( index ).getFirst().equals( "Steve" )
{
// do what you do when you find a list
// member with the desired name
}

Based on the way you've written the program so far, the hard part will be determining which characteristic of Student to search for, or check all of them figuring out how to normalize or adjust for the type of item you're searching for.

Re: Cannot search ArrayList in Java

Also, know of anyway I can edit my original post so nobody jacks my code? I assume many of colleges have this or similar assignments and I don't want anyone to cheat off of it.

I wouldn't worry about it too much. It's going to be similar to a lot of things that people are assigned; hopefully it is helpful to other people. As far as outright plagiarism, most professors have mechanisms to find it. Also, it seems so generic as to be unsearchable. I mean, if someone searched something like 'student class search homework help', this wouldn't even show up--sites that sell homework would.

There is an 'edit post' option, but it might not show up until you've made a certain number of posts or something--I'm not sure. But again, I really wouldn't worry about it.