Pagini recente » Cod sursa (job #413580) | Cod sursa (job #1072667) | Cod sursa (job #153136) | Cod sursa (job #2398876) | Cod sursa (job #1361143)
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new FileInputStream("convertor.in"));
sc.useDelimiter("}\\s*,");
// Lista are mai multe obiecte
// Un obiect are mai multe intrari
// O intrare are o pereche cheie-valoare
// Declarari si initializari
String obj;
String[] entries;
String key = "";
String value;
Pattern patternKey = Pattern.compile("\"([^\"\\(\\)]+)\"");
StringBuilder sb = new StringBuilder();
// Daca suntem in prima iteratie trebuie sa identificam cheile obiectelor
boolean first = true;
while (sc.hasNext()) {
obj = sc.next();
entries = obj.split("\\s*,\\s*");
if (first) {
for (String entry : entries) {
String[] entry2 = entry.split("\\s*:\\s*");
try {
Matcher matcher = patternKey.matcher(entry2[0]);
matcher.find();
key = matcher.group(0).replaceAll("\"", "");
} catch (IllegalStateException e) {}
//System.out.print(key + ",");
sb.append(key + ",");
}
first = false;
}
//System.out.println();
sb.append("\n");
for (String entry : entries) {
value = entry.substring(entry.indexOf(':') + 1);
value = value.replaceAll("[\"}\\]]", "").trim();
//System.out.print(value + ",");
sb.append(value + ",");
}
}
sc.close();
sc = null;
PrintWriter writer = new PrintWriter("convertor.out");
writer.write(sb.toString());
writer.close();
writer = null;
sb.delete(0, sb.length());
sb = null;
System.gc();
}
}