Results 1 to 6 of 6
  1. #1
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Help with a search please

    I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.

    Java Code:
    import java.util.Scanner;
    
    public class MyPeople {
    
    	public static void main(String[] args) {
    
    		Person[] p = new Person[] {
    				new Person("Chris", 26, "Male", "NJ", "Single"),
    				new Person("JoAnna", 23, "Female", "NJ", "Single"),
    				new Person("Dana", 24, "Female", "NJ", "Single"),
    				new Person("Dan", 25, "Male", "NJ", "Single"),
    				new Person("Mike", 31, "Male", "NJ", "Married") };
    
    
    		Scanner s = new Scanner(System.in);
    
    		String userInput = "";
    		int i = 0;
    		while (!userInput.equalsIgnoreCase("end")) {
    			userInput = s.nextLine();
    			for (i=0; i<p.length; i++){
    			while (!userInput.equalsIgnoreCase(p[i].getName()))
    				if (userInput.equalsIgnoreCase("end")) {
    					System.out.println("Program Terminated");
    				} else if (userInput.equalsIgnoreCase(p[i].getName())) {
    					System.out.println(p[i]);
    					System.out.println(i);
    				} else if (!userInput.equalsIgnoreCase(p[i].getName())) {
    					System.out.println("Not Found");
    					System.out.println(i);
    				} else {
    					break;
    				}
    			}
    		}
    
    	}
    
    }

  2. #2
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Help with a search please

    I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.

    Java Code:
    import java.util.Scanner;
    
    public class MyPeople {
    
    	public static void main(String[] args) {
    
    		Person[] p = new Person[] {
    				new Person("Chris", 26, "Male", "NJ", "Single"),
    				new Person("JoAnna", 23, "Female", "NJ", "Single"),
    				new Person("Dana", 24, "Female", "NJ", "Single"),
    				new Person("Dan", 25, "Male", "NJ", "Single"),
    				new Person("Mike", 31, "Male", "NJ", "Married") };
    
    
    		Scanner s = new Scanner(System.in);
    
    		String userInput = "";
    		int i = 0;
    		while (!userInput.equalsIgnoreCase("end")) {
    			userInput = s.nextLine();
    			
    				if (userInput.equalsIgnoreCase("end")) {
    					System.out.println("Program Terminated");
    				} else if (userInput.equalsIgnoreCase(p[i].getName())) {
    					System.out.println(p[i]);
    
    				} else if (!userInput.equalsIgnoreCase(p[i].getName())) {
    					System.out.println("Not Found");
    
    				} else {
    					break;
    				}
    		}
    
    	}

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with a search please

    Do you have a specific question?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help with a search please

    It doesn't work. What do I need to add?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with a search please

    You need to add some print statements at various locations in the logic so you can explain its behavior. Saying "It doesn't work" is not very helpful.
    The print statements should show the values of the variables as the program runs.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help with a search please

    'searching' implies some sort of loop where the program is actually trying to find a wanted value; I don't see any such construct in your code ...

    kind regards,

    Jos

    ps. @OP: I found your other similar thread and merged it with this thread.
    Last edited by JosAH; 11-19-2014 at 07:28 AM.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2012, 04:20 AM
  2. Replies: 0
    Last Post: 02-24-2012, 09:39 AM
  3. Replies: 4
    Last Post: 03-25-2011, 12:50 AM
  4. Replies: 0
    Last Post: 10-29-2009, 10:28 AM
  5. Replies: 5
    Last Post: 08-26-2008, 04:43 PM

Posting Permissions

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