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.

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.

Edited 6 Years Ago by Nick Evan: added code tags

I no I need to set up another scanner to read in the users choice but Im not sure how. Please help.

/*
An example that's been given hundreds of times
*/
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter one word:");
String input = keyboard.next();

And you should use two HashMaps, not one TreeMap. If you read the documentation on TreeMap you'll see that it takes log(n) time for get and put operations (the operations you'll be doing the most since you'll be putting things into the Map and looking them up a lot) whereas HashMap takes constant time. Of course there are a couple issues with HashMaps that can cause the time to not stay constant, but you needn't worry about that. The thing is it doesn't make sense to use a TreeMap if you don't provide a Comparator - read the Javadoc for TreeMap, it makes a huge deal about how it is always sorted, but your Map doesn't need to be sorted so why bother? Also, you'll need two Maps because Maps are looked up by Key, not by Value. You will need one Map that has the telephone number as the Key and the Name as the value, and you'll need another Map with the Name as the key and the telephone number as the value.

This article has been dead for over six months. Start a new discussion instead.