Write a program that converts the alphabetic characters in a text file into the
ID: 3624657 • Letter: W
Question
Write a program that converts the alphabetic characters in a text file into the corresponding words in the International Civil Aviation Organization (ICAO) alphabet. Your program should read the ICAO alphabet from the file called icaoAlphabet that can be found in the directory /home/venus/hilder/cs110Science/assignment5/datafiles. The file contains two lines of text (shown below):HotelDeltaNovemberUniformOscarAlphaLimaWhiskeyEchoQuebecZuluSierraBravo
JulietMikePapaYankeeCharlieFoxtrotXrayRomeoGolfKiloVictorIndiaTango
The beginning of each word in the ICAO alphabet is indicated by an uppercase letter. For example, the first word in the file is Hotel, the second is Delta, the third is November, etc. Extract each word from the file and insert it into an array corresponding to its position in the ICAO alphabet. For example, the word Hotel is the eighth word in ICAO alphabet, Delta is the fourth, November is the fourteenth, etc. After the words in the ICAO alphabet are loaded into the array, it should contain the words in the order shown below (array index values are shown):
[0]
[1] Alpha
[2] Bravo
[3] Charlie
.
.
.
[24] Xray
[25] Yankee
[26] Zulu
The array can now be used to convert the alphabetic characters in a text file into the corresponding words in the ICAO alphabet. Convert the text in the file called originalText that can be found in the directory /home/venus/hilder/cs110Science/assignment5/datafiles. Write the converted text to a file. For example, the first few words of your output file should appear as shown below:
WhiskeyRomeoIndiaTangoEcho Alpha PapaRomeoOscarGolfRomeoAlphaMike …
Submit your source code and a script that captures the following activities: compiling your program, executing your program, and a cat of your output file.
Explanation / Answer
This took a lot of effort. Please rate it well.
It works perfectly, I've tested it.
import java.io.*;
import java.util.*;
class Icao
{
static String [] arr=new String[27];
public static void store() throws IOException
{
BufferedReader b=new BufferedReader( new FileReader("icaoAlphabet.txt"));
String codes=b.readLine()+b.readLine()+"Z";
int last=0;
for(int x=1;x<codes.length();x++)
{
if(codes.charAt(x)>=65&&codes.charAt(x)<=90)
{
String word=codes.substring(last,x);
last=x;
arr[(int)word.charAt(0)-64]=word;
}
}
}
public static void main() throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println("input text to be encoded");
String text=s.nextLine().toUpperCase();
String encoded="";
store();
for(int x=0;x<text.length();x++)
{
if(text.charAt(x)!=' ')
encoded+=arr[(int)text.charAt(x)-64];
else
encoded+=text.charAt(x);
}
System.out.println(encoded);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.