Write a complete Java program to use Map to construct a dictionary for airport c
ID: 3721173 • Letter: W
Question
Write a complete Java program to use Map to construct a dictionary for airport code and airport city. The program should read from a text file (input.txt") to construct the dictionary and also all ow user to search for airport code based on airport city. Each line in the text file is a pair of airport code and city separated by a TAB key. Once the file is read and the dictionary is constructed, the program should ask user to enter an airport city and then search and display the code of corresponding airport(s) if found. The program will then decide whether to continue or not by asking user. For example, the user may type Chicago and it should return ORD The sample input file looks like this:
RDU Raleigh/Durham
SFO San Francisco
ORD Chicago
7. (16 pts) Write a complete Java program to use Map to construct a dictionary for airport code and airport city. The program should read from a text file (input.txt") to construct the dictionary and also all ow user to search for airport code based on airport city. Each line in the text file is a pair of airport code and city separated by a TAB key. Once the file is read and the dictionary is constructed, the program should ask user to enter an airport city and then search and display the code of corresponding airport(s) if found. The program will then decide whether to continue or not by asking user. For example, the user may type Chicago and it should return ORD The sample input file looks like this: RDU Raleigh/Durham SFO ORD Chicago San FranciscoExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package airport;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author
*/
public class Airport {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Map<String, String> dictionary = new HashMap<String, String>();
// The name of the file to open.
String fileName = "C:\Users\Faizan\Documents\NetBeansProjects\airport\src\airport\codes.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
String[] temp ;
while((line = bufferedReader.readLine()) != null) {
temp=line.split(" ");// there is a single space between code and airport name..
dictionary.put(temp[1].toLowerCase(), temp[0]); //temp1 contains airport name, temp[0] cpmtains code
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "' " + ex.getMessage());
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
boolean isExit =false;
int ans=0;
String airportName="";
Scanner s= new Scanner(System.in);
while (isExit==false) {
System.out.println("1.Search airport code 2: Exit. Enter 1 or 2");
ans=s.nextInt();
if (ans==1) {
System.out.println("Enter airport name:");
airportName=s.next();
System.out.println("Airport code : " + dictionary.get(airportName.toLowerCase()));
}else if (ans ==2){
isExit=true;
}else{
System.out.println("Enter correct input 1 or 2");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.