-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorseCodeConverter 2.java
177 lines (140 loc) · 6.14 KB
/
morseCodeConverter 2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Write a program that asks the user to enter a string, and then converts-
that string to Morse code and prints on the screen.
Use hyphens for dashes and periods for dots.
The Morse code table is given in a text file morse.txt.
When printing morse code, display eight codes on each line except the last line.
Codes should be separated from each other with one space.
There should be no extra spaces at the beginning and at the end of the output.
Uppercase and lowercase letters are translated the same way.
*/
/*
Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are -
represented by a series of dots and dashes. Write a program that asks the user to enter a file name containing morse -
code, and then converts that code to text and prints it on the screen. The Morse code table is given in a text file -
morse.txt. When printing resulting text, display one sentence on each line. There should be no extra spaces at the -
beginning and at the end of the output.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class morseCodeConverter {
public static void main(String[] args) throws FileNotFoundException {
String fileMorse = "morse.txt";
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the file name or type QUIT to exit:");
String fileToDecode = keyboard.nextLine();
String flag = fileToDecode.toLowerCase();
File checkFileForExist = new File(fileToDecode);
while (!flag.equals("quit")) {
if (!checkFileForExist.exists()) {
System.out.printf("File '%s' is not found.\n",fileToDecode);
System.out.println("Please re-enter the file name or type QUIT to exit:");
fileToDecode = keyboard.nextLine();
flag = fileToDecode.toLowerCase();
checkFileForExist = new File(fileToDecode);
}
else if (checkFileForExist.exists()){
String decodedString;
for (int i = 0; i < getCode(fileToDecode).size(); i++) {
decodedString = decodeMorse(nestedArrays(fileMorse), find2(getCode(fileToDecode).get(i),
nestedArrays(fileMorse)));
if (!checkSeparators(decodedString) ){
System.out.print(decodedString);
}else {
System.out.print(decodedString);
System.out.print("\n");
}
}
System.out.println();
System.exit(0 );
}
}
//ArrayList<ArrayList<String>> alphabet = new ArrayList<>(testNestedArray(fileMorse));
//String morses = convertStringToMorse(userInput, alphabet);
//ArrayList<String> testArray = new ArrayList<>(decodeMorse(fileToDecode));
//System.out.println(morses);
return;
}
private static boolean checkSeparators(String aa ){
boolean flag = false;
if (aa.equals(",") || aa.equals("?") ||aa.equals(".") ){
flag = true;
}
return flag;
}
private static String decodeMorse(ArrayList<ArrayList<String>> keysToDecode, int position) {
return keysToDecode.get(0).get(position);
}
private static String convertStringToMorse(String str, ArrayList<ArrayList<String>> alphabet) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
int position = find(c, alphabet);
if (position != 1) {
result.append(alphabet.get(1).get(position));
} else {
result.append('?');
}
result.append(' ');
}
return result.toString();
}
// find();
// Finds the position of character in the ArrayList
private static int find(Character c, ArrayList<ArrayList<String>> alphabet) {
boolean found = false;
int position = -1;
ArrayList<Character> charArray = new ArrayList<>();
for (int index = 0; index < alphabet.get(0).size(); index++) {
charArray.add(alphabet.get(0).get(index).charAt(0));
}
for (int i = 0; !found && i < alphabet.get(0).size(); i++) {
if (Character.toLowerCase(c) == Character.toLowerCase(charArray.get(i))) {
position = i;
found = true;
}
}
return position;
}
private static ArrayList<ArrayList<String>> nestedArrays(String fileName) throws FileNotFoundException {
Scanner myReader = new Scanner(new File(fileName));
ArrayList<String> alphabet = new ArrayList<>();
ArrayList<String> codes = new ArrayList<>();
while (myReader.hasNext()) {
String line = myReader.nextLine();
String[] tokens = line.split("[\t]");
alphabet.add(tokens[0]);
codes.add(tokens[1]);
}
myReader.close();
ArrayList<ArrayList<String>> myList = new ArrayList<>();
myList.add(alphabet);
myList.add(codes);
return myList;
}
private static ArrayList<String> getCode(String fileName) throws FileNotFoundException {
Scanner myData = new Scanner(new File(fileName));
ArrayList<String> arrayLists = new ArrayList<>();
while (myData.hasNext()) {
String line = myData.next();
arrayLists.add(line);
}
return arrayLists;
}
private static int find2(String elementInFile, ArrayList<ArrayList<String>> elementsToCompare) {
boolean found = false;
int position = 0;
ArrayList<String> elementArray = new ArrayList<>();
for (int index = 0; index < elementsToCompare.get(0).size(); index++) {
elementArray.add(elementsToCompare.get(1).get(index));
}
for (int i = 0; !found && i < elementsToCompare.get(0).size(); i++) {
if (elementInFile.equals(elementArray.get(i))) {
position = i;
found = true;
}
}
return position;
}
}