Witam
Doszedłem jak poradzić sobie z takim stringiem. Ale jak to zrobić z plikiem txt?
Pattern, matcher, delimiter scanner - to wszystko nie chce mi zadziałać razem Ratunku...
class QTimer {
private final long start;
public QTimer() {
System.gc();
start = System.currentTimeMillis();
}
public long getElapsed() {
return System.currentTimeMillis() - start;
}
}
public class Wycieczki {
public static void main(String[] args) throws Exception {
QTimer t = new QTimer();
Pattern p = Pattern.compile("\n");
String s = "Czerwony\n12\nBiały\n1\nZielony\n22\nCzarny\n8 ";
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
int count = 0;
while(m.find()) {
if(count++ % 2 != 1) {
m.appendReplacement(sb, " ");
}
}
m.appendTail(sb);
System.out.print(sb);
long etA = t.getElapsed();
System.out.println("\n Czas: "+etA +" ms");
}
}
WYNIK:
Czerwony 12
Biały 1
Zielony 22
Czarny 8
Czas: 5 ms