narenarya

Airport1.java

Apr 9th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. //AirportCodes1.java , Task ->2 , Date -> 09-04-2015
  2. import java.io.*;
  3. import java.util.Scanner;
  4. import java.util.StringTokenizer;
  5.  
  6. public class AirportCodes1
  7. {
  8.     public static void main (String[] args) throws IOException
  9.     {
  10.  
  11.         Scanner user_input = new Scanner( System.in );
  12.         String file_name , airport_name;
  13.  
  14.         //Take file name
  15.         System.out.print("Enter input file name >> ");
  16.         file_name = user_input.nextLine();
  17.        
  18.         //Take airport name
  19.         System.out.print("Enter airport name >> ");
  20.         airport_name = user_input.nextLine();
  21.  
  22.         //Input file
  23.         File file = new File(file_name);
  24.         BufferedReader buffer = new BufferedReader(new FileReader(file));
  25.         String line , token ,previous_token;
  26.         boolean IATA_code_found = false;
  27.         while ((line = buffer.readLine()) != null)
  28.         {
  29.            
  30.             line = line.trim();
  31.             //Start playing with trimmed line here
  32.  
  33.             StringTokenizer tokenizer = new StringTokenizer(line ,",");
  34.             //Tokenize the string and use them as instructed in question
  35.             //Previous token is cleverly storing the IATA code
  36.             int i=0;
  37.             previous_token = "";
  38.             while (tokenizer.hasMoreTokens())
  39.              {  
  40.                 token = tokenizer.nextToken().trim();
  41.                 if(i == 1)
  42.                 {
  43.                     if(token.equalsIgnoreCase(airport_name))
  44.                     {
  45.                         //You hit a jackpot ,just break from all loops after printing message
  46.                         System.out.println("Airport: "+ token + " has IATA code:"+ previous_token );
  47.                         IATA_code_found = true;
  48.                         break;
  49.                     }
  50.                 }
  51.  
  52.                 i++;
  53.                 previous_token = token;
  54.              }
  55.              if(i == 1)
  56.              break;
  57.         }
  58.  
  59.         //If no line is matching the given airport then display not found
  60.         if(!IATA_code_found)
  61.         {
  62.             System.out.println("Airport name: " + airport_name + " was not found");
  63.         }  
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment