Hallo,
wenn ich versuche, meine Java Klasse auszuführen, hängt es sich manchmal auf. Manchmal kommt es weiter und es wird etwas in der Konsole ausgegeben, manchmal wird gar nichts ausgegeben. Ich freue mich über jede Hilfe. Anbei mein Code. Ich greife mit Apache POI auf eine Excel Datei zu. Auch diese anbei.
Leider ist ein Screenshot von meinem Code zu groß, deswegen kann ich ihn hier nicht hochladen. Anbei deshalb ein Link zum code.
Vielen Dank im Voraus
Code: https://goonlinetools.com/snapshot/code/#w48wtlvv2zg9ewy8662wag
wenn ich versuche, meine Java Klasse auszuführen, hängt es sich manchmal auf. Manchmal kommt es weiter und es wird etwas in der Konsole ausgegeben, manchmal wird gar nichts ausgegeben. Ich freue mich über jede Hilfe. Anbei mein Code. Ich greife mit Apache POI auf eine Excel Datei zu. Auch diese anbei.
Leider ist ein Screenshot von meinem Code zu groß, deswegen kann ich ihn hier nicht hochladen. Anbei deshalb ein Link zum code.
Vielen Dank im Voraus
Code: https://goonlinetools.com/snapshot/code/#w48wtlvv2zg9ewy8662wag
Java:
package utils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;
public class ExcelUtils {
static HashMap<String, String> namesLetters = new HashMap<>();
static HashMap<String, Integer> namesNumber = new HashMap<>();
static HashMap<Integer, String> numberLetter = new HashMap<>();
static HashMap<Integer, String> numberNames = new HashMap<>();
static HashMap<String, String> pairs = new HashMap<>();
static HashMap<String, String> usedPairs = new HashMap<>();
static File file = new File("/Users/thierry/Desktop/Sitzplan/data/savedPairsHashMaps.rtf");
static Path filePath = Paths.get("/Users/thierry/Desktop/Sitzplan/data/savedPairsHashMaps.rtf");
static Charset charset = StandardCharsets.UTF_8;
static String excelPath = "/Users/thierry/Desktop/Sitzplan/data/Sitzplan Sitznachbarn.xlsx";
public static void main(String[] args) throws Exception {
removeTextFromTxt(file);
putHashMapData();
sitznachbarpaare();
}
public static void sitznachbarpaare() throws IOException {
double overallResult = 0;
double result1;
int randName1 = ThreadLocalRandom.current().nextInt(2, 32);
int randName2 = ThreadLocalRandom.current().nextInt(2, 32);
while (randName1 == randName2) {
randName1 = ThreadLocalRandom.current().nextInt(2, 32);
randName2 = ThreadLocalRandom.current().nextInt(2, 32);
}
int k = 0;
String[] splittedNames;
while (k < 30 * 29) {
for (int i = 0; i < 15; i++) {
double x = getCellDataByIndex(randName1, randName2);
double y = getCellDataByIndex(randName2, randName1);
result1 = x + y;
overallResult += result1;
String names = getNamesFromIndex(randName1, randName2);
splittedNames = names.split("\\s+");
pairs.put(splittedNames[0], splittedNames[1]);
randName1 = ThreadLocalRandom.current().nextInt(2, 32);
randName2 = ThreadLocalRandom.current().nextInt(2, 32);
do {
if (randName1 == randName2) {
randName1 = ThreadLocalRandom.current().nextInt(2, 32);
randName2 = ThreadLocalRandom.current().nextInt(2, 32);
}
}
while (pairs.containsKey(numberNames.get(randName1)) ||
pairs.containsKey(numberNames.get(randName2)) ||
pairs.containsValue(numberNames.get(randName1)) ||
pairs.containsValue(numberNames.get(randName2)) ||
usedPairs.containsKey(numberNames.get(randName1)) ||
usedPairs.containsKey(numberNames.get(randName2)) ||
usedPairs.containsValue(numberNames.get(randName1)) ||
usedPairs.containsValue(numberNames.get(randName2)));
System.out.println(pairs);
}
k++;
//System.out.println(pairs);
writePairsInTxt(pairs);
addUsedPairsFromTxt(usedPairs);
pairs.clear();
}
System.out.println(overallResult);
}
public static void addUsedPairsFromTxt(Map<String, String> map) {
try (BufferedReader br = Files.newBufferedReader(filePath, charset)) {
String line;
String[] names;
while ((line = br.readLine()) != null) {
names = line.split("\\s+");
map.put(names[0], names[1]);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void removeTextFromTxt(File file) throws FileNotFoundException {
PrintWriter writer = new PrintWriter(file);
writer.close();
}
public static void writePairsInTxt(Map<String, String> map) {
try (BufferedWriter bf = new BufferedWriter(new FileWriter(file, true))) {
for (Map.Entry<String, String> entry : map.entrySet()) {
bf.write(entry.getKey() + " " + entry.getValue());
bf.newLine();
}
bf.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getNamesFromIndex(int column, int row) {
String firstName = numberNames.get(column);
String secondName = numberNames.get(row);
return firstName + " " + secondName;
}
public static Double getCellDataByIndex(int excelcolumn, int excelrow) throws IOException {
FileInputStream inputstream = new FileInputStream(excelPath);
XSSFWorkbook workbook = new XSSFWorkbook(inputstream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
String ref = numberLetter.get(excelcolumn) + excelrow;
CellReference cr = new CellReference(ref);
row = sheet.getRow(cr.getRow());
cell = row.getCell(cr.getCol(), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
return cell.getNumericCellValue();
} else {
return 0.0;
}
}
public static void putHashMapData() {
namesLetters.put("Yuri", "B");
namesLetters.put("Thierry", "C");
namesLetters.put("Jennifer", "D");
namesLetters.put("Béla", "E");
namesLetters.put("Theodor", "F");
namesLetters.put("David", "G");
namesLetters.put("Yuliana", "H");
namesLetters.put("Ariana", "I");
namesLetters.put("Jonah", "J");
namesLetters.put("Alina", "K");
namesLetters.put("Thomas", "L");
namesLetters.put("Bjarne", "M");
namesLetters.put("Livia", "N");
namesLetters.put("Till", "O");
namesLetters.put("Jason", "P");
namesLetters.put("Justus", "Q");
namesLetters.put("Timofej", "R");
namesLetters.put("Nikolai", "S");
namesLetters.put("An", "T");
namesLetters.put("Khoi", "U");
namesLetters.put("Mai", "V");
namesLetters.put("Florian", "W");
namesLetters.put("Giuglio", "X");
namesLetters.put("Fridtjof", "Y");
namesLetters.put("Mark", "Z");
namesLetters.put("Markus", "AA");
namesLetters.put("Felix", "AB");
namesLetters.put("Max", "AC");
namesLetters.put("Daniil", "AD");
namesLetters.put("Yared", "AE");
namesNumber.put("Yuri", 2);
namesNumber.put("Thierry", 3);
namesNumber.put("Jennifer", 4);
namesNumber.put("Béla", 5);
namesNumber.put("Theodor", 6);
namesNumber.put("David", 7);
namesNumber.put("Yuliana", 8);
namesNumber.put("Ariana", 9);
namesNumber.put("Jonah", 10);
namesNumber.put("Alina", 11);
namesNumber.put("Thomas", 12);
namesNumber.put("Bjarne", 13);
namesNumber.put("Livia", 14);
namesNumber.put("Till", 15);
namesNumber.put("Jason", 16);
namesNumber.put("Justus", 17);
namesNumber.put("Timofej", 18);
namesNumber.put("Nikolai", 19);
namesNumber.put("An", 20);
namesNumber.put("Khoi", 21);
namesNumber.put("Mai", 22);
namesNumber.put("Florian", 23);
namesNumber.put("Giuglio", 24);
namesNumber.put("Fridtjof", 25);
namesNumber.put("Mark", 26);
namesNumber.put("Markus", 27);
namesNumber.put("Felix", 28);
namesNumber.put("Max", 29);
namesNumber.put("Daniil", 30);
namesNumber.put("Yared", 31);
numberLetter.put(1, "A");
numberLetter.put(2, "B");
numberLetter.put(3, "C");
numberLetter.put(4, "D");
numberLetter.put(5, "E");
numberLetter.put(6, "F");
numberLetter.put(7, "G");
numberLetter.put(8, "H");
numberLetter.put(9, "I");
numberLetter.put(10, "J");
numberLetter.put(11, "K");
numberLetter.put(12, "L");
numberLetter.put(13, "M");
numberLetter.put(14, "N");
numberLetter.put(15, "O");
numberLetter.put(16, "P");
numberLetter.put(17, "Q");
numberLetter.put(18, "R");
numberLetter.put(19, "S");
numberLetter.put(20, "T");
numberLetter.put(21, "U");
numberLetter.put(22, "V");
numberLetter.put(23, "W");
numberLetter.put(24, "X");
numberLetter.put(25, "Y");
numberLetter.put(26, "Z");
numberLetter.put(27, "AA");
numberLetter.put(28, "AB");
numberLetter.put(29, "AC");
numberLetter.put(30, "AD");
numberLetter.put(31, "AE");
numberNames.put(2, "Yuri");
numberNames.put(3, "Thierry");
numberNames.put(4, "Jennifer");
numberNames.put(5, "Béla");
numberNames.put(6, "Theodor");
numberNames.put(7, "David");
numberNames.put(8, "Yuliana");
numberNames.put(9, "Ariana");
numberNames.put(10, "Jonah");
numberNames.put(11, "Alina");
numberNames.put(12, "Thomas");
numberNames.put(13, "Bjarne");
numberNames.put(14, "Livia");
numberNames.put(15, "Till");
numberNames.put(16, "Jason");
numberNames.put(17, "Justus");
numberNames.put(18, "Timofej");
numberNames.put(19, "Nikolai");
numberNames.put(20, "An");
numberNames.put(21, "Khoi");
numberNames.put(22, "Mai");
numberNames.put(23, "Florian");
numberNames.put(24, "Giuglio");
numberNames.put(25, "Fridtjof");
numberNames.put(26, "Mark");
numberNames.put(27, "Markus");
numberNames.put(28, "Felix");
numberNames.put(29, "Max");
numberNames.put(30, "Daniil");
numberNames.put(31, "Yared");
}
/*
public static Double getCellDataByName(String nameCol, String nameRow) throws IOException {
FileInputStream inputstream = new FileInputStream(excelPath);
XSSFWorkbook workbook = new XSSFWorkbook(inputstream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
String ref = names_letters.get(nameCol) + names_number.get(nameRow);
CellReference cr = new CellReference(ref);
row = sheet.getRow(cr.getRow());
cell = row.getCell(cr.getCol(), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
return cell.getNumericCellValue();
} else {
return 0.0;
}
}
*/
}
Anhänge
Zuletzt bearbeitet: