1

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");
    }
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
  • Why don't you try contacting string and translation and store them at the same index? – JAMSHAID Feb 10 '20 at 13:12
  • You should combine word and translation together in a single entry. Maybe a custom class with two fields, or a Pair or even a `String[2]`. Otherwise you have to be very careful not to mess up the order. – Thilo Feb 10 '20 at 13:13
  • thank you but how do I do this given I have the two array list entries: al.add(word); al.add(translation); –  Feb 10 '20 at 13:38
  • Don't have two array list entries, have only one: `al.add(new String[]{ word, translation });` or `al.add(Pair.of(word, translation));` or `al.add(new WordWithTranslation(word, translation));` – Thilo Feb 10 '20 at 13:48

1 Answers1

1

Arranging Strings in an Alphabetical Order with Java 7 (Classic way):

for (int i = 0; i < count; i++) {
    for (int j = i + 1; j < count; j++) { 
        if (str[i].compareTo(str[j])>0) {
            temp = str[i];
            str[i] = str[j];
            str[j] = temp;
        }
    }
}

Sorting the strings Java 8:

arrayList.sort((p1, p2) -> p1.compareTo(p2));

Using Comparator Java 8:

arrayList.sort(Comparator.comparing(MyObject::getA));

Find word Java 7:

List <String> listClone = new ArrayList<String>(); 
for (String string : list) {
    if(string.matches("(?i)(text).*")) {
        listClone.add(string);
    }
}

Using a java.util.HashSet:

Set<String> set = new HashSet<String>(list);
if (set.contains("text")) {
    System.out.println("String found!");
}

Using contains:

for (String s : list) {
    if (s.contains("text")) {
        System.out.println(s);
    }
}

Find word Java 8:

List<String> matches = list.stream()
                           .filter(it -> it.contains("txt")) 
                           .collect(Collectors.toList());

Performance of contains() in a HashSet vs ArrayList:

  • The contains() method works faster in HashSet compared to an ArrayList

Reference:

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58