Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AirportCodes1.java , Task ->2 , Date -> 09-04-2015
- import java.io.*;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- public class AirportCodes1
- {
- public static void main (String[] args) throws IOException
- {
- Scanner user_input = new Scanner( System.in );
- String file_name , airport_name;
- //Take file name
- System.out.print("Enter input file name >> ");
- file_name = user_input.nextLine();
- //Take airport name
- System.out.print("Enter airport name >> ");
- airport_name = user_input.nextLine();
- //Input file
- File file = new File(file_name);
- BufferedReader buffer = new BufferedReader(new FileReader(file));
- String line , token ,previous_token;
- boolean IATA_code_found = false;
- while ((line = buffer.readLine()) != null)
- {
- line = line.trim();
- //Start playing with trimmed line here
- StringTokenizer tokenizer = new StringTokenizer(line ,",");
- //Tokenize the string and use them as instructed in question
- //Previous token is cleverly storing the IATA code
- int i=0;
- previous_token = "";
- while (tokenizer.hasMoreTokens())
- {
- token = tokenizer.nextToken().trim();
- if(i == 1)
- {
- if(token.equalsIgnoreCase(airport_name))
- {
- //You hit a jackpot ,just break from all loops after printing message
- System.out.println("Airport: "+ token + " has IATA code:"+ previous_token );
- IATA_code_found = true;
- break;
- }
- }
- i++;
- previous_token = token;
- }
- if(i == 1)
- break;
- }
- //If no line is matching the given airport then display not found
- if(!IATA_code_found)
- {
- System.out.println("Airport name: " + airport_name + " was not found");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment