Learning Java

Discussion in 'Java' started by Magnus, Nov 28, 2003.

  1. Magnus

    Magnus Guest

    Hi!

    I'm a totally idiot in the java language so far, because I started to
    learn it yesterday. I want to create an applet, which works as a
    phone-book. I used the treeSet method, to creat a list, wich I can add
    name and number to. My problem is now: How can I search in this list?
    I want to write the name of the person, and get the number. I would be
    so grateful if any professional here wants to help me. I include my
    code:

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;

    public class Telefonbok extends Applet implements
    ActionListener {

    TreeSet treeSet= new TreeSet();

    TextField input1= ( new TextField( 20));
    TextField input2= ( new TextField( 20));
    TextArea output1= ( new TextArea( 10, 33));
    TextArea output2= ( new TextArea( 10, 33));

    Button knapp1;
    Button knapp2;


    public void init(){

    setLayout ( new BorderLayout ());
    Panel io= new Panel ();
    io.setLayout ( new GridLayout ( 3,2));

    io.add ( new Label ( "Namn:"));
    io.add ( input1);
    io.add ( new Label ( "Nummer:"));
    io.add ( input2);
    knapp2= new Button ("Sök");
    io.add ( knapp2);
    knapp1= new Button ( "Lägg till");
    io.add ( knapp1);
    input1.addActionListener( this);
    input2.addActionListener( this);
    knapp1.addActionListener( this);
    knapp2.addActionListener( this);
    add( io, BorderLayout.NORTH);
    add( output1, BorderLayout.EAST);
    add( output2, BorderLayout.WEST);

    }

    public void actionPerformed( ActionEvent ae){

    String namn1= input1.getText();
    namn1= namn1.trim();

    String nummer1= input2.getText();
    nummer1= nummer1.trim();

    if ( ae.getSource()==knapp1)
    treeSet.add( namn1 + " " + nummer1);


    input1.setText("");
    input2.setText("");
    output1.setText( "");


    Iterator lista1= treeSet.iterator();
    while( lista1.hasNext())
    output1.append( lista1.next()+"\n");



    }
    }
     
    Magnus, Nov 28, 2003
    #1
    1. Advertising

  2. Magnus

    Jose Rubio Guest

    You're better off using a HashMap instead of a Set. Since names are not
    unique, you might want to use the phone number as the key in the map. Then
    to search by name you would do something like:

    //The phone book
    Map phonebook = new HashMap();

    //Some entries
    phonebook.put( "312-456-9999","John");
    phonebook.put( "773-889-1234","Mary");

    //To search for a name
    boolean found = String phonebook.containsValue( "John" );

    //Now you can get a set of the entries and loop through them and find
    the one you need.
    Set entries = phonebook.entrySet();
    Iterator it = entries.iterator();

    while( it.hasNext() )
    {
    Map.Entry current = (Map.Entry)it.next();
    if( current.getValue() == "John" )
    {
    //Match
    String number = (String)current.getKey();
    }
    }

    I haven't compiled or tried this code out, but it should give you an idea.
    If you can live with the name being the key, the search is alot easier. So
    for example if you wante to search for a number and get the name it would
    be:

    String name = (String)phonebook.get( "312-456-9999" );

    if( name != null )
    {
    //It's a match, do whatever you need to do
    }

    Hope it helps!

    Jose

    "Magnus" <> wrote in message
    news:...
    > Hi!
    >
    > I'm a totally idiot in the java language so far, because I started to
    > learn it yesterday. I want to create an applet, which works as a
    > phone-book. I used the treeSet method, to creat a list, wich I can add
    > name and number to. My problem is now: How can I search in this list?
    > I want to write the name of the person, and get the number. I would be
    > so grateful if any professional here wants to help me. I include my
    > code:
    >
    > import java.awt.*;
    > import java.applet.*;
    > import java.awt.event.*;
    > import java.util.*;
    >
    > public class Telefonbok extends Applet implements
    > ActionListener {
    >
    > TreeSet treeSet= new TreeSet();
    >
    > TextField input1= ( new TextField( 20));
    > TextField input2= ( new TextField( 20));
    > TextArea output1= ( new TextArea( 10, 33));
    > TextArea output2= ( new TextArea( 10, 33));
    >
    > Button knapp1;
    > Button knapp2;
    >
    >
    > public void init(){
    >
    > setLayout ( new BorderLayout ());
    > Panel io= new Panel ();
    > io.setLayout ( new GridLayout ( 3,2));
    >
    > io.add ( new Label ( "Namn:"));
    > io.add ( input1);
    > io.add ( new Label ( "Nummer:"));
    > io.add ( input2);
    > knapp2= new Button ("Sök");
    > io.add ( knapp2);
    > knapp1= new Button ( "Lägg till");
    > io.add ( knapp1);
    > input1.addActionListener( this);
    > input2.addActionListener( this);
    > knapp1.addActionListener( this);
    > knapp2.addActionListener( this);
    > add( io, BorderLayout.NORTH);
    > add( output1, BorderLayout.EAST);
    > add( output2, BorderLayout.WEST);
    >
    > }
    >
    > public void actionPerformed( ActionEvent ae){
    >
    > String namn1= input1.getText();
    > namn1= namn1.trim();
    >
    > String nummer1= input2.getText();
    > nummer1= nummer1.trim();
    >
    > if ( ae.getSource()==knapp1)
    > treeSet.add( namn1 + " " + nummer1);
    >
    >
    > input1.setText("");
    > input2.setText("");
    > output1.setText( "");
    >
    >
    > Iterator lista1= treeSet.iterator();
    > while( lista1.hasNext())
    > output1.append( lista1.next()+"\n");
    >
    >
    >
    > }
    > }
     
    Jose Rubio, Nov 28, 2003
    #2
    1. Advertising

  3. "Magnus" <> wrote in message
    news:...
    > Hi!
    >
    > I'm a totally idiot in the java language so far, because I
    > started to learn it yesterday. I want to create an
    > applet, which works as a phone-book.
    >


    Since you are inexperienced in using the Java language I would recommend
    against building an applet [or any GUI-based application for that matter] as
    your first project. Instead, concentrate on learning the language by writing
    console-based Java applications, that is, those that use 'System.out' and
    'System.err' for I/O.

    You might want to try some of the tutorials at:

    http://java.sun.com/docs/books/tutorial/index.html

    or Google-search for similar items.

    As for your project, it can be more easily tackled if you realise it
    consists of the building of two, quite separate, but interoperable,
    sub-systems:

    * A data management sub-system [phone list-specific code]
    * A user interface sub-system [applet-specific code]

    >
    > I used the treeSet method, to creat a list, wich I can
    > add name and number to. My problem is now: How
    > can I search in this list? I want to write the name of the
    > person, and get the number. I would be so grateful if
    > any professional here wants to help me. I include my
    > code:
    >

    <SNIP CODE>
    >


    The specifics of using the Java API are found in the relevant documentation.

    However, following on from what was said earlier, why not create a 'phone
    list manager' class that, internally, uses some sort of 'Collection'-based
    class for storage, and allows you to perform common tasks such as:

    * Adding a new entry
    * Deleting an existing entry
    * Finding a specific entry [by name, by number, etc]
    and retrieving it for reading, or editing
    * Listing all entries

    and so on. You can test this class in a console-based application, and once
    satisfied that it works, can then commence the task of building an applet
    that interfaces to this 'phone list manager'.

    In other words, first design, build and test your data management
    sub-system, then tackle the complexities of building a GUI-based user
    interface sub-system. Trust me when I say that system development will
    proceed much more smoothly if this approach is adopted.

    I hope this helps.

    Anthony Borla
     
    Anthony Borla, Nov 28, 2003
    #3
  4. "Anthony Borla" <> wrote in message
    news:_Mzxb.30290$...
    ....
    >...You can test this class in a console-based application, and once
    > satisfied that it works, can then commence the task of building an applet


    ...or local application, or server side application..

    > that interfaces to this 'phone list manager'.
    >
    > In other words, first design, build and test your data management
    > sub-system, then tackle the complexities of building a GUI-based user
    > interface sub-system. Trust me when I say that system development will
    > proceed much more smoothly if this approach is adopted.


    Very well stated, Anthony.

    [ My recent encounters with J2EE have reminded
    me of the importance of designing for the console
    _1st_. ]

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Nov 28, 2003
    #4
  5. "Andrew Thompson" <> wrote in message
    news:rVBxb.30408$...
    > "Anthony Borla" <> wrote in message
    > news:_Mzxb.30290$...
    > ...
    > >...You can test this class in a console-based application,
    > > and once satisfied that it works, can then commence the
    > > task of building an applet

    >
    > ..or local application, or server side application..
    >


    Indeed !

    > > that interfaces to this 'phone list manager'.
    > >
    > > In other words, first design, build and test your data
    > > management sub-system, then tackle the complexities
    > > of building a GUI-based user interface sub-system.
    > > Trust me when I say that system development will
    > > proceed much more smoothly if this approach is
    > > adopted.

    >
    > Very well stated, Anthony.
    >
    > [ My recent encounters with J2EE have reminded
    > me of the importance of designing for the console
    > _1st_. ]
    >


    It's often the lessons associated with pain, and frustration [sometimes even
    hair loss !!!] that are the ones best absorbed, and retained ;) !

    Cheers,

    Anthony Borla
     
    Anthony Borla, Nov 28, 2003
    #5
  6. Magnus

    Chris Smith Guest

    Jose Rubio wrote:
    > You're better off using a HashMap instead of a Set. Since names are not
    > unique, you might want to use the phone number as the key in the map. Then
    > to search by name you would do something like:
    >
    > //The phone book
    > Map phonebook = new HashMap();
    >
    > //Some entries
    > phonebook.put( "312-456-9999","John");
    > phonebook.put( "773-889-1234","Mary");


    I must differ here. The fact that names are not unique is significant,
    but the proposed solution throws away the whole value of the data
    structure in question. Instead, you probably want to use a Map which
    maps (name) to (list of phone numbers). If you need reverse lookup from
    phone number to name, the you can *also* maintain a reverse index like
    that.

    Using a Map in the wrong direction, though, results in code that's
    slower and harder to write than simply keeping the data in a list.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Nov 28, 2003
    #6
  7. Magnus

    Jose Rubio Guest

    I Agree! If the bulk of the lookups are by name the approach you mention
    will be more efficient.

    --
    Jose Rubio
    Lead Consultant
    Airphoria
    http://www.airphoria.com

    "Chris Smith" <> wrote in message
    news:4.net...
    > Jose Rubio wrote:
    > > You're better off using a HashMap instead of a Set. Since names are not
    > > unique, you might want to use the phone number as the key in the map.

    Then
    > > to search by name you would do something like:
    > >
    > > //The phone book
    > > Map phonebook = new HashMap();
    > >
    > > //Some entries
    > > phonebook.put( "312-456-9999","John");
    > > phonebook.put( "773-889-1234","Mary");

    >
    > I must differ here. The fact that names are not unique is significant,
    > but the proposed solution throws away the whole value of the data
    > structure in question. Instead, you probably want to use a Map which
    > maps (name) to (list of phone numbers). If you need reverse lookup from
    > phone number to name, the you can *also* maintain a reverse index like
    > that.
    >
    > Using a Map in the wrong direction, though, results in code that's
    > slower and harder to write than simply keeping the data in a list.
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
     
    Jose Rubio, Nov 28, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    7
    Views:
    505
  2. Hayak
    Replies:
    1
    Views:
    486
    BigSargeone
    Apr 17, 2007
  3. Begin Learning Java
    Replies:
    5
    Views:
    624
    Arne Vajhøj
    Mar 17, 2010
  4. Lew
    Replies:
    0
    Views:
    413
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    781
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page