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);
}
}