I have written the constructor below that takes a word from a file, passes it to an external method 'Translate' that returns a translation of the word.
In parallel (and for code that I have not jet fully written) the constructor takes a string word as an that it will (once the code is written) find the word in the dictionary.
But before I do that I need to put both the word and the translation in an ArrayList. I know that a Map would be better but I need to use an ArrayList.
My code does this but there is something that I do not understand.
I write the word to the array list and then the translation....so I would expect the ArrayList to be Word1,Translation1,Word2,Translation2,
But when I run my print command it prints all the words and then all the translations...
The reason that I am trying to fathom this out is that I want to be able to sort the array list on word (the dictionary is unsorted) and then look up an individual word....and quickly pick up its translation
So my question is - am I using the ArrayList correctly (accepting the limitations of the ArrayList for this exercise and how can I sort using word as the key?
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
class Translate {
String original;
String translation;
public Translate(String original) throws FileNotFoundException {
this.original = original;
this.translation = translation;
ArrayList al = new ArrayList();
Path p1 = Paths.get("C:/Users/Green/documents/dictionary.txt");
Scanner sc = new Scanner(p1.toFile()).useDelimiter("\\s*-\\s*");
while (sc.hasNext()) {
String word = (sc.next());
String translation = (Translate(word));
al.add(word);
al.add(translation);
System.out.println("Print Arraylist using for loop");
for(int i=0; i < al.size(); i++) {
System.out.println( al.get(i));
}
}
}
public static void main(String args[]) throws FileNotFoundException {
Translate gcd = new Translate("envolope");
}
}