1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-11-13 07:33:44 +01:00

Import from other formats

This commit is contained in:
Carlos Galindo 2016-06-12 16:04:56 +02:00
parent 637bc33d2d
commit 034464b7b9
Signed by: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 26 additions and 4 deletions

View file

@ -86,10 +86,17 @@ public class Controller implements Initializable {
@FXML
private void onImportAction(ActionEvent event) {
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv)", "*.csv"));
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv, .txt)", "*.csv", "*.txt"));
File file = chooser.showOpenDialog(root.getScene().getWindow());
if (file == null) return;
List<TestItem> aux = TestItem.loadFrom(file, TestItem.SEMICOLON);
String separator;
if (file.getName().matches(".*txt"))
separator = TestItem.TAB;
else if (file.getName().matches(".*csv"))
separator = TestItem.SEMICOLON;
else
separator = TestItem.COMMA;
List<TestItem> aux = TestItem.loadFrom(file, separator);
if (aux != null) {
tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux));
tabPane.getSelectionModel().selectLast();

View file

@ -10,7 +10,7 @@ import java.util.List;
import java.util.Scanner;
public class TestItem {
public static final String COMMA = ",", COLONS = "::", SEMICOLON = ";";
public static final String TAB = "\t", COLONS = "::", SEMICOLON = ";", COMMA = ",";
private StringProperty question, answer;
@ -67,12 +67,27 @@ public class TestItem {
public static List<TestItem> loadFrom(File file, String separator) {
List<TestItem> list = new ArrayList<>();
try {
Scanner in = new Scanner(file);
Scanner in;
switch (separator) {
case COMMA:
case SEMICOLON:
case TAB:
in = new Scanner(new FileInputStream(file), "ISO-8859-1");
break;
default:
in = new Scanner(file);
break;
}
while (in.hasNextLine()) {
String line = in.nextLine();
if (!line.matches("..*" + separator + "..*")) continue;
int index = line.indexOf(separator);
String question = line.substring(0, index);
String answer = line.substring(index + separator.length());
index = answer.indexOf(separator);
if (index != -1)
answer = answer.substring(0, index);
if (answer.isEmpty()) continue;
list.add(new TestItem(question, answer));
System.err.println(question + " :: " + answer);
}