a.txt :
1,2,3
1,2,6
2,4,5
Reading and splitting
String wynik = readFileAsString("a.txt");
String[] wyniki = wynik.split("");
I can't seem to find a way to split String out after newLines that are in file, and not specified like this (this is what I found on all other examples on stackoverflow):
1,2,3\n1,2,6\n2,4,5\n
My readFileAsString works correctly, and it actually reads it that way (without splitting):
1,2,31,2,62,4,5
I know I could add something to my file, to make it split by it, eg
1,2,3t
...
But I also need that file to be clear and without any unnecessary symbols.
Entire thing looks like this:
public static String readFileAsString(String file) {
String line, results = "";
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
// readLine() zwraca nulla na koncu pliku, stad zabezpieczenie
while ((line = reader.readLine()) != null) {
results += line;
}
reader.close();
// return results;
} catch (IOException e) {
System.err.println("Error");
}
return results;
}
public static void main(String args[]) {
String wynik = readFileAsString("a.txt");
System.out.println(wynik);
String[] wyniki = wynik.split("[\\r\\n]+");
ArrayList<Trojka> wynikArrList = new ArrayList<Trojka>();
for (int i = 0; i < wyniki.length; i++) {
String[] temp1 = wyniki[i].split(",");
int m = 0;
int n = 0;
int t = 0;
try {
m = Integer.parseInt(temp1[0]);
} catch (Exception e) {
}
try {
n = Integer.parseInt(temp1[1]);
} catch (Exception e) {
}
try {
t = Integer.parseInt(temp1[2]);
} catch (Exception e) {
}
Trojka temp = new Trojka(m, n, t);
boolean newRecord = true;
for (Trojka a : wynikArrList) {
if (a.getM() == temp.getM() && a.getN() == temp.getN()) {
a.addT((int) temp.getT());
newRecord = false;
break;
}
}
if (newRecord) {
wynikArrList.add(temp);
}
}
wynik = "";
for (Trojka a : wynikArrList) {
wynik += a.getM() + "," + a.getN() + "," + a.getT() + "\n";
}
System.out.println(wynik);
}
}