Results 1 to 2 of 2
  1. #1
    sean12 is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Help telephone book application using maps

    Im having trouble with an assignment that I was given. here is the assignment and the code I have so far.

    The Telephone Lookup application

    The task
    The goal of this assignment is to design and write a program that does a telephone lookup. The program reads a data set of names and telephone numbers from a file that contains the names and telephone numbers in random order. Your program must handle lookups by name and also reverse lookups by phone number.

    The specifics
    You are to design and write a program that prompts the user for the name of the input file that contains names and telephone numbers of individuals. The file is NOT organized in any particular fashion (i.e. sorted). The program should offer the user a menu of choices as to looking up a telephone number given a name of an individual or looking up a name given a telephone number, or quitting. You must use Maps to solve this problem,

    Your tasks

    1. Input:
    The text will come from an input file. (Use any input files to test your program but I will use my own to test.) The input file has the format:

    Cannon, Arthur
    278-0946
    Abbott, Dan
    637-4093

    The user should be prompted for the name of the file, and then given the choices of 1) lookup by name, 2) lookup by number, 3) quit. The program should run over and over until the user chooses quit.

    2. Output:
    If the user chooses 1) lookup by name, the output is the telephone number displayed on the console.
    If the user chooses 2) lookup by number, the output is the name displayed on the console.
    If the user chooses 3) quit, the program should terminate.

    3. Requirements:
    a. Write the algorithm for solving the problem 1st . Include it in the comment section of your code.
    b. You must write the driver class called TelephoneLookup that :
    • Reads data from a file
    • Gives the user a menu of 3 choices: look up by name, look up by number, or quit
    • Creates one or more Maps to store the names and associated telephone numbers.
    • Based upon the choice above, does the appropriate action.
    • Note: since you’re using Maps to solve this problem, you will NOT need any other classes other than the driver.
    c. You can use any input file you'd like but I will use my own to test the program.

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.util.TreeMap;
    import java.util.Map;
    
    
    public class TelephoneLookup
    {
       public static void main(String[] args)
       {
          Scanner console = new Scanner(System.in);
          try
          {
             Scanner in = new Scanner(new File("TextIn.txt"));
    
             Map<String, String> map = new TreeMap<String, String>();
    
             while(in.hasNext())
             {
                String name = in.nextLine();
                String number = in.nextLine();
                map.put(name, number);
             }
             //System.out.println(map);
    
             boolean done = false;
    
             while(!done)
             {
                System.out.println("Enter name if you want to loop up telephone number by name,");
                System.out.println("Enter phone if you want to loop up name by telephone number,");
                System.out.println("Enter quit if your done");
                String input = in.next();
    
                if (input.equalsIgnoreCase("quit"))
                {
                   done = true;
                }
    
                else if (input.equalsIgnoreCase("name"))
                {
                  System.out.println("Please enter the name");
                  String nameInput= in.next();
                }
    
                else if (input.equalsIgnoreCase("phone"))
                {
                  System.out.println("Please enter the phone number");
                  String numberInput= in.next();
                }
    
             }
             //out.println(map);
             }//end try
    
             catch(FileNotFoundException e)
             {
               System.err.println("Cannot find input file " );
               System.exit(1);   // abnormal termination status code
             }
             catch(IOException e)
             {
               System.err.println("Cannot open input file " );
               System.exit(2);
             }
       }
    
       }
    I no I need to set up another scanner to read in the users choice but Im not sure how. Please help.

    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-19-2010 at 11:06 PM. Reason: Moderator Edit: Code tags added

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    ok I have never used Maps before but I think I have a working solution for ya

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.util.TreeMap;
    import java.util.Map;
    
    
    public class TelephoneLookUp
    {
    public static void main(String[] args)
    {
    Scanner console = new Scanner(System.in);
    Scanner in;
    //use this scanner to get user input from console
    Scanner keyboard=new Scanner(System.in);
    try {
            //note I used my own file you will need to change probably
    	in = new Scanner(new File("Telephone"));
    
    
    Map<String, String> map = new TreeMap<String, String>();
    //created a second map for reverse look up
    Map<String, String> map2=new TreeMap<String, String>();
    while(in.hasNext())
    {
    String name = in.nextLine();
    String number = in.nextLine();
    map.put(name, number);
    map2.put(number,name);
    }
    //System.out.println(map);
    
    boolean done = false;
    
    while(!done)
    {
    System.out.println("Enter name if you want to loop up telephone number by name,");
    System.out.println("Enter phone if you want to loop up name by telephone number,");
    System.out.println("Enter quit if your done");
    String input = keyboard.next();
    
    if (input.equalsIgnoreCase("quit"))
    {
    done = true;
    }
    
    else if (input.equalsIgnoreCase("name"))
    {
    System.out.println("Please enter the name");
    String nameInput= keyboard.next();
    readName(nameInput,map);
    }
    
    else if (input.equalsIgnoreCase("phone"))
    {
    System.out.println("Please enter the phone number");
    String numberInput= keyboard.next();
    readNumber(numberInput,map2);
    }
    
    }}catch (FileNotFoundException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
    //out.println(map);
    }
    	public static void readName(String name,Map m)
    	{
    		boolean match=false;
    		Scanner in;
    		try {
    			in = new Scanner(new File("Telephone"));
    		
    		while(in.hasNext())
    		{
    			String data=in.next();
    			//System.out.println(data);
                             if(data.equals(name){
    			for(int i=0;i<m.size();i++)
    			{
    				if(m.containsKey(data))
    				{
    				System.out.println(m.get(data));
    				match=true;
    				break;
    				}
    			}
                            }
    			if(match)
    			{
    				break;
    			}
    		}} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    
    	public static void readNumber(String number,Map m)
    	{
    		boolean match=false;
    		Scanner in;
    		try {
    			in = new Scanner(new File("Telephone"));
    		
    		while(in.hasNext())
    		{
    			String data=in.nextLine();
    			
    			if(data.equals(number))
    			{
    				for(int i=0;i<m.size();i++)
    				{
    					if(m.containsKey(data))
    					{
    						System.out.println(m.get(data));
    						match=true;
    						break;
    					}	
    				}
    			}
    			if(match)
    			{
    				break;
    			}
    		}} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    }
    well it works anyway
    Last edited by al_Marshy_1981; 04-19-2010 at 11:16 PM. Reason: fixed a bug

Similar Threads

  1. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 06:13 PM
  2. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 09:36 AM
  3. Serial port programming using modem and telephone
    By maskey_dipesh in forum Advanced Java
    Replies: 1
    Last Post: 09-03-2009, 11:53 AM
  4. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 03:59 AM
  5. Google Maps API
    By mew in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 10:28 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
  •