Cod sursa(job #1359652)

Utilizator tudoras8tudoras8 tudoras8 Data 25 februarie 2015 00:41:46
Problema Convertor Scor 50
Compilator java Status done
Runda rosedu_cdl_2015 Marime 1.19 kb
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(new FileInputStream("convertor.in"));
		sc.useDelimiter("}\\s*,");

		PrintWriter writer = new PrintWriter("convertor.out");
		// Un obiect are mai multe intrari, iar fiecare intrare are o pereche cheie-valoare
		boolean first = true;
		while (sc.hasNext()) {
			String obj = sc.next();
			String[] entries = obj.split("\\s*,\\s*");
			if (first) {
				for (String entry : entries) {
					String[] entry2 = entry.split("\\s*:\\s*");

					Pattern patternKey = Pattern.compile("\"([a-zA-Z0-9\\s]+)\"");
					Matcher matcher = patternKey.matcher(entry2[0]);
					matcher.find();

					String key = matcher.group(0).replaceAll("\"", "");
					//System.out.print(key + ",");
					writer.write(key + ",");
				}
				first = false;
			}
			//System.out.println();
			writer.write("\n");
			for (String entry : entries) {
				String[] entry2 = entry.split("\\s*:\\s*");
				String value = entry2[1].replaceAll("[\"}\\]]", "").trim();
				//System.out.print(value + ",");
				writer.write(value + ",");
			}
		}

		sc.close();
		writer.close();
	}
}