1

List of russian names from input txt file

  • Александр
  • Роман
  • Михаил

This code sorts these names correctly in IntelliJ Idea during debugging. When I create a jar file and run it from the windows console java -jar E:\\sort-it.jar, then in the output file the first name is Роман, although it should be Александр, as in debugging.

The incorrect order from jar launch is

  • Роман
  • Александр
  • Михаил

The correct order is

  • Александр
  • Михаил
  • Роман

What could be the problem?

package programs;

import java.io.*;
import java.util.*;

public class Main{

  public static String inputFileName = "E:/in.txt";
  public static String outputFileName = "E:/out.txt";

  public static List<String> FetchFileData(String fileName) throws IOException {
    List<String> tempArray = new ArrayList();
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line;
    while ((line = reader.readLine()) != null){
        tempArray.add(line);
    }
    reader.close();
    return tempArray;
  }

  public static List<String> SortWords(List<String> inputArray) {
    String temp;
    for (int i = 0; i < inputArray.size(); i++){
        for (int j = i + 1; j < inputArray.size(); j++){
            if (inputArray.get(i).compareTo(inputArray.get(j)) > 0){
                temp = inputArray.get(i);
                inputArray.set(i, inputArray.get(j));
                inputArray.set(j, temp);
            }
        }
    }
    return inputArray;
  }

  public static void WriteToFile(List<String> inputArray, String fileName) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    for (int i = 0; i < inputArray.size(); i++) {
        writer.write(inputArray.get(i));
        writer.newLine();
    }
    writer.close();
  }

  public static void main(String[] args) throws IOException {
    List<String> unsortedArray;
    List<String> sortedArray;                
    unsortedArray = FetchFileData(inputFileName);
    sortedArray = SortWords(unsortedArray);
    WriteToFile(sortedArray, outputFileName);
  }
}
Fasta
  • 13
  • 2
  • Maybe you can use a Collator as shown here: https://stackoverflow.com/a/7633680/4636715 This way you add the Locale to the code explicitly rather than relying on the İntelliJ's one. – vahdet Aug 25 '17 at 14:23
  • But if I need write sort algorythm manually, how I can improve my code? – Fasta Aug 25 '17 at 14:43

1 Answers1

1

A small problem is that FileReader uses the default platform encoding. In the IDE & Windows that could be another that in the console. Better do:

public static List<String> FetchFileData(String fileName) throws IOException {
    Charset charset = Charset.forName("Cp1251");
    return Files.readAllLines(Paths.get(fileName), charset);
}

Specifying the charset of your files ensures that the application is portable to other computers (with the same file). Files provides support for writing too.

Ensure that every line trimmed of spaces and maybe the Unicode BOM character, \uFEFF:

String line = lines.get(i);
line = line.trim().replace("\uFEFF", "");

That there are better solutions that compareTo has already been said.

No sneeky Latin lookalike letters were inserted instead of Cyrillic.

The code looks fine too.

So check the charset; something else I do not see, as unlikely as it is.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138