Results 1 to 9 of 9
Thread: Search through objects
- 04-14-2009, 03:00 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Search through objects
Hello, I am in need of some help with a program I am writing.
Before I describe the issues I've been having , I'll go over what the program is to do.
The program is essentially a scoring program that takes the name entered in a text field (GUI is set up using BreezySwing) and searches through all 'person' objects to find the one with that persons name (the one entered in the text field). After that object has been successfully found, a score is sent to the object (<object name>.setScore(score); ). What I'm having trouble with is writing a method to find the object with the individuals name in it. In other words, how do I search through a list of objects of all the same type? How would I go about naming those objects if I don't know how many there will be? Also, is it possible to use a String variable to name an object? Thank you all in advance for your help.
Marcus
P.S. The objects would be able to receive a getName() method and return a String containing an individual's name.
-
1) How are the Person objects stored? What type of collection? An array? LinkedList? ArrayList?
2) Does your Person class have a getName() or similar method where you can extract the person's name to compare with your String?
In all likelihood, you'd create a method that iterates through your collection of Persons with a for loop, checking if each person is the one of interest, and if found return it. Something like:
Java Code:public Person findPerson(String name) { // assuming your collection of Persons is called // personCollection // and assuming Person method getName() for (Person p: personCollection) { if (p.getName.equalsIgnoreCase(name)) { return p; // return found Person } } // if person with name not found, return null return null; }
-
Ah Marcus, cross-posting the same question in multiple forum without letting us know can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.
New To Java - Searching through objects
- 04-14-2009, 04:01 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
My apologies, I'm new to the whole forum thing. I will remember to do that in the future. (To be honest, I wasn't sure how soon someone would respond). Anyway, thank you very much for your help.
Marcus
-
No problem. Now you know. Best of luck.
- 04-14-2009, 04:48 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
I actually have one more question. Another component of my program loads a file and transfers that data to an array. When I go to search it with another method the array cannot be found. I'm not quite sure what to do. Could you perhaps give me some pointers? Thank you.
Marcus
P.S. Here is the code I am working on: (The file that is loaded uses comma separated values, it looks like: "firstname lastname,city,school" and so forth)
Java Code:import java.util.*; import java.io.*; import java.awt.*; import TerminalIO.*; public class ArrayWrite3{ public static void main (String args[]){ KeyboardReader reader = new KeyboardReader(); String fileName = reader.readLine("What is the name of the file?: "); readFile(fileName); searcher(); } public static void readFile(String fileName) { String[][] iArray = new String[32][3]; try { Scanner scanner = new Scanner(new File(fileName + ".txt")); scanner.useDelimiter(","); for(int i = 0;i<=31;i++){ for(int q = 0;q<=2;q++){ iArray[i][q] = new String (scanner.next()); System.out.print(iArray[i][q] + " "); if (q == 2){ System.out.println(); } } } scanner.close(); } catch (FileNotFoundException e){ e.printStackTrace(); } } public static void searcher(){ KeyboardReader reader = new KeyboardReader(); String searchVar = reader.readLine("Individuals name please: "); for(int i = 0;i<=31;i++){ for(int q = 0;q<=2;q++){ if((iArray[i][q]).equals(searchVar)){ System.out.println(i + " " + q); } } } } }Last edited by MarcusD; 04-14-2009 at 04:51 AM.
-
iArray is declared within the readFile method and thus exists only in that method. So logically, Outside of that method and within other methods such as searcher, there is no such object as iArray.
to solve this, you need to increase the scope of the iArray variable. Declare it in the class like so:
Java Code:public class ArrayWrite3 { public static String[][] iArray = new String[32][3]; public static void main(String args[]) { KeyboardReader reader = new KeyboardReader(); String fileName = reader.readLine("What is the name of the file?: "); readFile(fileName); searcher(); } public static void readFile(String fileName) { // String[][] iArray = new String[32][3]; try { Scanner scanner = new Scanner(new File(fileName + ".txt")); scanner.useDelimiter(","); // ..... etc .....
- 04-14-2009, 05:03 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Absolutely wonderful, it works! Thank you very much!
Marcus
-
Similar Threads
-
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
how to search for DXF?
By fishtoprecords in forum Suggestions & FeedbackReplies: 0Last Post: 11-29-2008, 09:43 PM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM -
Search
By Gilgamesh in forum New To JavaReplies: 5Last Post: 11-26-2007, 03:45 AM -
Web Search
By Marcus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-02-2007, 06:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks