Can't Search Trhough an Array Twice

Hey all.
I'm trying to write a a sort of phonebook program that takes in the first name and the last name of a person, and then searches trough the phonebook for it. This is the program:
class PhoneEntry
  String FirstName;    // first name of a person
  String LastName;     // last name of a person
  String phone;        // their phone number
  PhoneEntry( String f, String s, String p )
    FirstName = f; LastName = s;  phone = p;
class PhoneBook
PhoneEntry[] phoneBook;
PhoneBook()    // constructor
   phoneBook = new PhoneEntry[ 5 ] ;
   phoneBook[0] = new PhoneEntry(
    "James", "Barclay", "(418)665-1223");
   phoneBook[1] = new PhoneEntry(
    "Grace", "Dunbar", "(860)399-3044");
   phoneBook[2] = new PhoneEntry(
    "Paul", "Kratides", "(815)439-9271");
   phoneBook[3] = new PhoneEntry(
    "Violet", "Smith", "(312)223-1937");
   phoneBook[4] = new PhoneEntry(
    "John", "Wood", "(913)883-2874");
PhoneEntry search( String targetFirstName ) 
   for (int j=0; j<phoneBook.length; j=j+1)
     if ( phoneBook[ j ].
       FirstName.equals( targetFirstName))
           return phoneBook[ j ];
   return null;
PhoneEntry search2( String targetLastName ) 
   for (int j=0; j<phoneBook.length; j=j+1)
     if ( phoneBook[ j ].
       FirstName.equals( targetLastName))
           return phoneBook[ j ];
   return null;
import java.lang.*;
import java.io.*;
class PhoneBookTester3
public static void main (String[] args) throws IOException
   PhoneBook pb = new PhoneBook(); 
   InputStreamReader inStream =
        new InputStreamReader( System.in ) ;
    BufferedReader stdin =
        new BufferedReader( inStream );
   String inputData; String FirstName; String LastName;
    System.out.println( "Enter first name:" );
    FirstName = stdin.readLine();
    PhoneEntry entry = pb.search( FirstName );
    System.out.println( "Enter last name:" );
    FirstName = stdin.readLine();
    PhoneEntry entry = pb.search( LastName );
   if ( FirstName.equals("quit") )
     System.out.println( "Good-bye." );
   if ( LastName.equals("quit") )
     System.out.println( "Good-bye." );
   else
      if ( entry != null )
          System.out.println( entry.FirstName + " " + entry.LastName + ":" + entry.phone );
      else
        if ( entry != null )
                System.out.println( entry.FirstName + " " + entry.LastName + ":" + entry.phone );
        else
                System.out.println("Name not found.");
}Each class is in its own window and is compiled seperately but they're conneted. I'm having a problem with the last class. I'm having a problem with this bit of code:
System.out.println( "Enter last name:" );
FirstName = stdin.readLine();
PhoneEntry entry = pb.search( LastName );It tells me that "entry" is already defined, and I get that, but if I change it the search doesn't seem to work properly. Does anyone know hoe I could get this program to work so that the user enters the first and last names and then the program searches for both?

This logic is a bit screwy, too:
if ( entry != null )
          System.out.println( entry.FirstName + " " + entry.LastName + ":" + entry.phone );
      else //[i.e., if entry is null]
        if ( entry != null ) // This will never be true.
                System.out.println( entry.FirstName + " " + entry.LastName + ":" + entry.phone );
        else // This will always be true
                System.out.println("Name not found.");
       }I think you need to find first name and last name in one call to some sort of search method. Your logic doesn't make sense. It should be:
prompt for first name
prompt for last name
search through list for a match of "first name and last name" (one call, two parameters [combine search and search2--both names must match]).

Similar Messages

  • Search on an Array

    Hi,
    I was wandering if there are not any other way, faster than linear search, to search an unordered array.

    You can't prevent checking the same index. And for
    the case where the element does not exist, you do not
    know when to terminate because you will never find it.
    Of course you could use a boolean array to mark
    weather a specific index location has already been
    queried, but then you need twice the amount of space.
    Plus the overhead of generating random numbers. The
    e house always wins... :-(Hmm, well that explains why the probabilistic search method is seldom used -:)
    It would actually be quite easy to chose indices randomly and still visit each element just once (it involves swapping of visited elements) if that was the problem. But it isn't. A random visiting order would actually be equivalent to a sequential visting order of randomly ordered elements. No gain just pain -:)

  • Search 1-D Array not working

    Hi all,
    Can anyone help me with the attached VI?  The "Search 1-D Array" does not seem to be searching.  The output is always -1, when I know that my data from my excel sheet that I am reading from contains the number 193.3359.  I intend to find how many rows in my excel data contains the number 193.3359, and I would like an output array to copy each row & all it's info.  (example: if there were 6 rows that contain the number 193.3359, i would like all 6 rows show up in the output array).
    Thank you for your help.  I do not have a sample of my data, but it's about 2,000 rows and 10 columns.  They contain numbers & words.  Here I am just searching for a number.
    Attachments:
    Search Excel Data.vi ‏12 KB

    Taki1999 wrote:
    I'm guessing that you have a precision issue.
    Are you sure that the number is coming back as 193.3359 and not 193.335899999?
    No, he's searching for raw strings so this cannot be it. This would be an issues with e.g. DBLs
    Some other guesses:
    The strings in the array elements contain leading or trailing spaces or other nonprintable characters.
    The file has comma as decimal seperator (e.g. from a german computer).
    LabVIEW Champion . Do more with less code and in less time .

  • Search in an array of cluster

    Hi!
    Is there a smart way to search in an array of clusters.
    For example: You have an array of a cluster with a digital control and a string control.
    And you want to search for a specific value in the digital control.
    What is the best way to that?
    Is there a way, instead of a foor-loop and unbundle, like search 1-D array where you can ignore the string value.
    If I use the search function I need to specify both the numeric and the string, but I'm only intrested in the numeric.

    If you are always wanting to search this array based on the value of one particular value in the cluster, something you can do is when creating the cluster array, create two arrays that relate to each other on an element-by-element basis.
    One array contains the values that you are going to want to search. The other array contains a cluster with all the other values. Searching the "key" array gives you an index that you can use to index the correct element from the other data array.
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • Can I search for multiple images simultaneously in Aperture?

    Can I search for multiple images simultaneously in Aperture?  For example, search for 'Image2.jpg, Image3.jpg, Image5.jpg' by image name, out of different folders? 

    dusan -- your best procedure is likely to be to type the first file name (without extension) in the search field of the Photos View, and then mark that file either with a Flag or a color label.  (You might clear all Flags prior.)  If you have a lot of images or a slow computer (or both ) you may have to wait a few seconds.  Aperture will show "Loading ... " on the left side of the tool strip while it churns through your images.
    Then hit backspace twice, and type "60".  Mark the file found.
    Repeat with variations.
    Then go to Flagged view, or filter the whole Photos view for the color-label you used.

  • Search in object array

    I am working on a program in which one of the problems is to search an object array to allow a user to edit an item in the array. I have examples on how to do linear and binary searches, but they are all simple strings of numeric data. How do I apply this algorithm to a more complex array?I have a class file called Application that contains my constructor, and my get & set methods for creating the objects in the array. And I have a driver class to allow a user to view the array, add items to it , and edit items in it.
    This is an example of my declaration of the initial items:
    Application inventory[] = new Application[100];
      inventory[0] = new Application("Jumpstart Toddlers", 14.99, 500);
      inventory[1] = new Application("Norton Antivirus 2003", 49.99, 1200);
      etc....
    I would like to set it up so that the user is prompted for the name of the software item they would like to edit, the array is searched using a method which returns the index the name was found in.

    Sounds like a homework assignment to me but...
    Appliction apps[] = new Application[100];
    ...Add Items to the apps array...
    String software_to_search_for = .. user entered
    data....
    for (int i = 0; i < apps.length; ++i)
    if
    f
    (apps.getSoftware().equalsIgnoreCase(software_to_sea
    ch_for))
    edit(apps[i], software_to_search_for);
    public void edit(Application app, String newName)
    // You can do anything here
    app.setName(newName);
    } // edit
    True enough, this is an assignment. But..These assignments were given to us without all the necessary tools. They are set up by the dept. and we have to finish them even if the instructor does not finish covering all the relevant material.In this case, we have not covered the equalsIgnoreCase(something obviously necessary for my problem). Now that I know what I am looking for, I have found it in my book and can apply it to my code appropriately. I was not necessarily looking for code, just the tools I need for my own design. This is something that the Computer dept. allows for in that they have assistants in the open labs that will help in this manner. Problem is, everyone has been out for Thanksgiving since Tues. Please dont feel as though you assisted someone in cheating!! I can assure you, that is not the case here. Thank you for your help.

  • Searching through an array of objects

    Hi, I am currently working on an assignment where I have to create a class called Persons with data fields of Name(String),age(int),income(int).
    All the values including the string values for name and values for income and age are all randomly generated and are stored in an array of objects of class "Persons".
    The array size is user specified.
    What I must attempt to implement now is to make a search feature that lets the user search through the array of objects for a particular name.
    I'm having some bad luck trying to implement this feature.
    If anyone can help me, I would greatly appreciate that!
    Cheers.

    Hi, Thank you for the prompt reply.
    I do not yet know HOW to start this,I mean I know how to search for individual characters in an element of an array, but to search for whole strings I am quite unsure.
    public class Persons
        //data fields
        private String name;
        private int age;
        private int income;
        //Constructor for Class Persons
        public Person(String[] name,int[] age,int[] income)
              String[] tempName = name;
              int[] tempAge = age;
              int[] itempIncome = income;
        //Instance method for returning some basic values about personal data
        public void toString(Persons ofChoice)
            System.out.println("Persons Data");
            System.out.println("Name: " + ofIntrest.name);
            System.out.println("Age: " + ofIntrest.age);
            System.out.println("Income: $" + ofIntrest.income);
    }This is my Persons class code, but I am quite stumped how to search for whole strings at the time being.

  • How can I pass an empty array to a parameter of type PLSQLAssociativeArray

    How can I pass an empty array to a parameter of type PLSQLAssociativeArray in VB? I defined the parameter like this
    Dim myArray() as String = new String() {}
    Dim myPara as new Oracle.DataAccess.Client.OracleCollectionType.PLSQLAssociativeArray
    myPara = 0
    myPara.Value = myArray
    When I execute my stored procedure giving the above parameter, I got error saying OracleParameter.Value is invalid.
    I have tried to give it the DBNull.Value, but it doesn't work either.
    Note: everything works fine as long as myArray has some item in there. I just wonder how I can make it works in case I have nothing.
    Thank you,

    How can I pass an empty array to a parameter of type PLSQLAssociativeArray in VB? I defined the parameter like this
    Dim myArray() as String = new String() {}
    Dim myPara as new Oracle.DataAccess.Client.OracleCollectionType.PLSQLAssociativeArray
    myPara = 0
    myPara.Value = myArray
    When I execute my stored procedure giving the above parameter, I got error saying OracleParameter.Value is invalid.
    I have tried to give it the DBNull.Value, but it doesn't work either.
    Note: everything works fine as long as myArray has some item in there. I just wonder how I can make it works in case I have nothing.
    Thank you,

  • I can't search through my Purchased content in the iOS 6 App Store.

    In iOS 5 I could go to the Purchased area and push down for a search bar to show up. But in iOS 6 there is nothing there.
    I clean out room on my iPhone with the knowledge that if I really need this app later that I can just reinstall it. Now that is going to be a hassle scrolling through all my bought app's. Sometimes I can't remember the name of something I had once had and if I could sort them into categories that would be helpful as well. But the main point is that I can't search, also when I do find something there and I press it it just shows like the version information. It use to take me to the app page so I can make sure that was what I was looking for. I have a lot of photo app's and I can not be expected to know what everyone of those do.
    I hate posting on forums because I like to find the information myself. But I cannot find this answer. How do I search my Purchased app in iOS 6?

    Yup, the new itunes is worthless unless every song you have is in your itunes library as well. (unless you buy every song from itunes this is simply impossible)  Apparently Apple threw away the feature in an effort to force everyone to purchase their media through itunes.  This is probably the worst idea about the new Itunes, the only way to go back is to restore your computer to a time before you downloaded the new version, otherwise wait and hope the next version in a year or so will have it.

  • I can't search any videos on YouTube/Download any app from the App Store

    Hi,
    I am using movistar 3G network (Spain), full signal. Everything works fine, Stock app, Weather app, Mail, Safari... but I can't search for any videos on YouTube and can't download any app from the App Store.
    I can see videos on YouTube app but only the featured videos, when I try to search the ones I want to see I get an error message "Cannot connect to YouTube".
    The only problem is when searching videos on YouTube app.
    When I try to download an app from the App Store I get a similar error message, "The download can´t be done, connect to iTunes" more or less, that's the sense of the message.
    I can connect to the AppStore, surf it but when I press Install... I get the error message.
    I know that my carrier, movistar (Spain) has an issue with secure webs (https), it's impossible to surf any secure web by now but I don´t know if this is related with my problems.
    This happens with 2.1 and 2.2 firmware version.
    I have the iPhone set in spanish from Spain and the time is right. By the way, using WiFi everything works fine, everything, I can search for videos on YouTube app and download apps from the AppStore.
    I don´t know what more can I do, can someone help me? please. = (
    Thanks in advance.

    FWIW, the only part of Java not installed is the Java Runtime Environment. Launch the Java Preferences.app in /Utilities and DL it when prompted. Then, enable plugins and Web Start apps.

  • How can i search for a color that is not in the color palette? (CS 5.5)

    I have a client, that has used a certain color in a series of textfields throughout the document.
    The color consists of all 4 CMYK colors and needs to be just Keycolor. However the color is not in the swatches menu, so marking it there and changing it won't work. I have downloaded a script that might have worked, but it could only find colors that were in the swatches palette. (Find / Change by Color / Overprint" version 1.2).
    Ofcourse, creating a similar color and adding it to the swatches doesn't work.
    Is there anyway that i can search for the color or does it really have to be done manually.
    Best regards

    You can only search for real, actually existing swatches. No sweat: use "Add Unnamed Colors" in the Swatch panel dropdown menu to get a list of all of the manually applied colors.
    Since you want to change all of them to the same color, you don't even have to use Search/Replace. Simply delete the new colors and select the one you need in the "Replace With" dialog that appears.

  • IPad version:how can I search a word on modules?

    Hi to everybody! How can I search a word in modules?
    I understand that I can do a search in spreadsheet, but I need to do in a module that is more efficient...so do you know?
    And why when I write on module I can't press enter and begin to write a new line??
    Thanks!

    Touch the URL field at the top of the page, type your word and a list of possible searches will be shown. Near the bottom of this list will be "On this page" which shows hits on the page (if any). Touch this and you will be taken to the first match.

  • Can I search email archive on iCloud server from iOS device, or can I only search what it has downloaded locally?

    Can I search email archive on iCloud server from iOS device, or can I only search what it has downloaded locally?
    I currently have an iPhone and iPad, and am soon changing over to a Mac. When that happen I'd like to try to see what I can migrate over from the Google ecosystem to the iCloud ecosystem.
    1) Currently I have the Gmail iOS app (a webapp container) that can search server-side of several years of email archive.  Can iCloud do this using the iOS native mail application?  Or is the search just limited to what the device has downloaded locally?
    2) Do I have to use a @me.com address for the full iCloud email sync to take place between server, Mac, and iOS devices?
    Believe it or not I've read quite a bit about the iCloud services, but the answers to these questions were far from clear.  Many thanks for the help.

    2)  iCloud only syncs email between .me/.mac addresses.  For other accounts like google, you set up the account in the mail app and if they're IMAP accounts, you can sync between the google server and any devices you have defined that account on.
    I'm not absolutely sure about 1) since I haven't had to search going back years - I use MailSteward for that on the mac.  On my iPhone/iPad, when I go into a mailbox and the emails are not immediately available, it downloads them.  I suspect for a search, you need the emails downloaded.

  • Can I search (find) my ipad from my mac air?

    Can I search (find) my ipad from my mac air? I know there is an app for iphone or ipad but, how you do it from a Mac Air?

    Absolutely you can. It is part and parcel to iCloud's functionality on the Air.
    Set-up iCloud on the Mac and log into iCloud. The i{ad itself must also be properly registered with your Apple ID.

  • HT204053 Can I search or locate different iPhone devices using one apple ID?

    Hello!
    Can I search or locate different iphone devices using one apple I.D.?
    Thanks!

    You can only use Find My iPhone for accounts that you can log into.  If you and your neighbor have different IDs and you only know your ID, than you can only track devices attached to your account.

Maybe you are looking for