views:

160

answers:

3

Write a program to keep your friends telephone numbers. You should be able to; Add a new name and number (Look up a number given a name) Save the data to an object file when you terminate the programme Restore the saved data when the program is rerun

the part i am confused about is in the brackets above

class CarMain { public static void main (String[] args) throws IOException, ClassNotFoundException { List< Friend> Friends = new ArrayList< Friend>();

  Friends.add(new Friend("blah","blah" 1234567));
  Friends.add(new Friend("blah","blah" 1234567));
  Friends.add(new Friend("blah","blah" 1234567)); 
  Friends.add(new Friend("blah","blah" 1234567)); 
  Friends.add(new Friend("blah","blah" 1234567));

  for (int i = 0; i<Friends.size(); i++)
   System.out.println(Friends.get(i).toString() );
  System.out.println("Friends set up");

    ObjectOutputStream out = null;
    try       {
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("FriendFile.txt")));
        out.writeObject(Friends);
    } 
    finally    {
        out.close();
    }

    ObjectInputStream in = null;
    try      {
        in = new ObjectInputStream(new
                BufferedInputStream(new FileInputStream("FriendFile.txt")));
         List<Friend> newFriends = (List<Friend>) in.readObject();

         for (int i = 0; i<newFriends.size(); i++)
      System.out.println(newFriends.get(i).toString() );
    } 
    finally    { 
        in.close();
    }

}//end main }//end class CarMain

//import java.io.*;

class Friend implements Serializable { private String fname, lname; private int number;

public Friend(String fna, String lna,  int num)     {
 lname= lna;
    fname = fna;
 number=num;

}
public String getFname()   {
 return fname;
}

public String getLname ()  {
 return lname;
}
public int getNumber()  {
 return number;
}

   public String toString()  {
   String out = "Last Name: " + fname + "\t First Name: " + lname + "\t Telephone number: "+ number  ;
   return out;
}

}

+1  A: 

At least have an attempt before posting homework requests here.

Quick Joe Smith
dude read, its simple, all i am asking is how to find a particular name in an text . jeeez
brent
This should have been a comment.
BalusC
At the time I did not have an "add comment" link for anybody else's post.
Quick Joe Smith
A: 

As you didn't elaborate the actual coding problem, I can't go in depth about this. It is not clear where you are stuck. If you just don't know where to start, then I think java.util.Properties is a nice and easy start, assuming that all of those friends have an unique name. Here's a tutorial about that API.

If you are not allowed to use that API, then left behind the java.util.Map API (tutorial here) in combination with Java IO API (tutorial here). As it is character data, you need a Reader and a Writer. Good luck.

BalusC
+1  A: 

You question doesn't really have anything to do with BufferedWriters or any other IO - you can do it all in memory.

You've got a list of friends and you want to search for a friend with a given name. There are two basic approaches to this: either you could just look through the whole list, or you could build a Map<String, Friend>. If you're going to do repeated lookups with a long list, you would probably want to use the second option - but if you're just looking up a single number, there's no benefit in going through the list to build up a map first. I would keep it simple.

So, you have a List<Friend> and a name to look up. You've already got code to loop through the list, so you can use the same sort of loop to try to find a friend by name:

public static void displayNumber(List<Friend> friends, String searchName)
{
    for (int i = 0; i < friends.size(); i++)
    {
         Friend friend = friends.get(i);
         if (friend.getFname().equals(searchName))
         {
             System.out.println(friend.getNumber());
             // Remove the return statement if you want to display *all*
             // the phone numbers matching a given first name
             return;
         }
    }
}

A few things to note:

  • This only matches by first name; if you want to match by last name instead or as well, you'll have to amend the code. You should be able to work out how though.
  • There's no null check here - if the first name of a friend is null, or if one of the entries in the list is null, (or if the list itself is null) this will throw a NullPointerException.
  • Consider the comment in the middle - what do you want to happen if there are multiple friends with the same name?
  • "getFname" isn't the most descriptive method name in the world; I'd suggest "getFirstName" instead.
  • This code can be made a bit simpler using the enhanced for loop from Java 5:

    for (Friend friend : friends)
    {
         if (friend.getFname().equals(searchName))
         // Code as before
    }
    
  • I would also suggest using camel case for your local variables in your main method - so I'd call the list variable friends rather than Friends.

  • Finally, in your finally blocks you should check for null before calling close(), as otherwise if the file can't be opened at all you'll get a NullPointerException. Alternatively, put the variable initialization before the try block.
Jon Skeet
thank you very much you have been very helpfull.
brent