Cod sursa(job #1361783)

Utilizator DD1994Mincu Dragos DD1994 Data 25 februarie 2015 23:42:04
Problema Convertor Scor 30
Compilator java Status done
Runda rosedu_cdl_2015 Marime 2.54 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		
		List<String> keys = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		
		Scanner reader = null;
		PrintWriter writer = null;
		String line;
		String word;
		boolean save, isValue;
		int currentCharIndex;
		int objectIndex;
		
		try {
			reader = new Scanner(new FileInputStream("convertor.in"));
			writer = new PrintWriter("convertor.out");
			objectIndex = -1;
			while (reader.hasNextLine()) {
				line = reader.nextLine();
				currentCharIndex = 0;
				word = "";
				save = false;
				isValue = false;
				while (currentCharIndex < line.length()) {
					if (line.charAt(currentCharIndex) == ' ' && save == false) {
						//just skip
					} else if (line.charAt(currentCharIndex) == ':') {
						isValue = true;
					} else if (line.charAt(currentCharIndex) == '{') {
						objectIndex++;
						if (objectIndex > 0) {
							if (objectIndex == 1) {
								for (String key : keys) {
									writer.print(key + ",");
								}
								writer.println();
							}
							for (String value : values) {
								writer.print(value + ",");
							}
							writer.println();
						}
						keys = new ArrayList<String>();
						values = new ArrayList<String>();
					} else if ((line.charAt(currentCharIndex) == '"' || Character.isDigit(line.charAt(currentCharIndex))) && save == false) {
						word = ""; save = true;
						if (Character.isDigit(line.charAt(currentCharIndex))) {
							word += line.charAt(currentCharIndex);
						}
					} else if ((line.charAt(currentCharIndex) == ',' || line.charAt(currentCharIndex) == '"' || line.charAt(currentCharIndex) == '}') && save == true) {
						if (isValue) {
							values.add(word);
						} else {
							if (objectIndex == 0) {
								keys.add(word);
							}
						}
						word = ""; save = false; isValue = false;
					} else if (save == true) {
						word += line.charAt(currentCharIndex);
					}
					currentCharIndex++;
				}
			}
			if (objectIndex == 1) {
				for (String key : keys) {
					writer.print(key + ",");
				}
				writer.println();
			}
			for (String value : values) {
				writer.print(value + ",");
			}
			writer.println();
		} catch (FileNotFoundException e) {
			System.out.println("Input file not found!");
		} finally {
			reader.close();
			writer.close();
		}
	}
}