Results 1 to 9 of 9
  1. #1
    MarcusD is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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

  4. #4
    MarcusD is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    No problem. Now you know. Best of luck.

  6. #6
    MarcusD is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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 .....

  8. #8
    MarcusD is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Absolutely wonderful, it works! Thank you very much!

    Marcus

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  2. how to search for DXF?
    By fishtoprecords in forum Suggestions & Feedback
    Replies: 0
    Last Post: 11-29-2008, 09:43 PM
  3. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  4. Search
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 03:45 AM
  5. Web Search
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-02-2007, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •