Cod sursa(job #1361109)

Utilizator cristian.panaitescuCristian Panaitescu cristian.panaitescu Data 25 februarie 2015 19:43:18
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 2.66 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner br = new Scanner(new FileReader("convertor.in"));
		PrintWriter wr = new PrintWriter(new FileWriter("convertor.out"));
		//BufferedReader br = new BufferedReader(new FileReader("input2"));
		String line = null;
		String word = "";
		int i = 0;
		boolean searchForKeys = true;
		boolean shouldEndLine = false;

		line = br.nextLine();
		char c;
		while (true) {

			c = line.charAt(i);
			if (c == '}') 
				shouldEndLine = true;
			if(searchForKeys == true)
				if (c == '\"') {
					i++;
					word = "";
					while (line.charAt(i) != '\"') {
						word += line.charAt(i);
						i++;
	
					}
					if (c == '}') 
						shouldEndLine = true;
					wr.print(word + ",");
					searchForKeys = false;
				}
			i++;
			if (c == '}' || shouldEndLine == true) {
				wr.println("");
				shouldEndLine = false;
				break;
			}
			
			if(c == ',')
				searchForKeys = true;
			
			if (i == line.length()) {
				line = br.nextLine();
				i = 0;
			}
		}
		br.close();
		
		br = new Scanner(new FileReader("convertor.in"));
		i = 0;
		line = br.nextLine();
		while (true) {

			c = line.charAt(i);
			if (c == ':') {
				i++;
				if (i == line.length()) {
					line = br.nextLine();
					i = 0;
				}
				word = "";
				
				while (line.charAt(i) != '\0' && line.charAt(i) != ',' && line.charAt(i) != '}') {
					if(line.charAt(i) == '}')
						shouldEndLine = true;
					word += line.charAt(i);
					i++;
					if (i == line.length()) {
						line = br.nextLine();
						i = 0;
					}

				}
				if(line.charAt(i) == '}')
					shouldEndLine = true;
				wr.print(print(word) + ",");
				
			}
			i++;
			if (i == line.length()) {
				line = br.nextLine();
				i = 0;
			}
			c = line.charAt(i);
			if (c == '}' || shouldEndLine == true) {
				wr.println("");
				shouldEndLine = false;
			}
			
			
			if (c == ']') {
				wr.println("");
				break;
			}
			
			
		}
		br.close();
		
	}

	private static String print(String word) {
		int i = 0;
		String forPrint = "";
		while (true) {
			if (word.charAt(i) == '"') {
				i++;
				while(word.charAt(i) != '"') {
					forPrint += word.charAt(i);
					if(i < word.length() - 1)
						i++;
					else 
						break;
				}
				
				return forPrint;
			}
			while(word.charAt(i) >= '0' && word.charAt(i) <= '9') {
				forPrint += word.charAt(i);
				if(i < word.length() - 1)
					i++;
				else 
					break;
			}
			if(i < word.length() - 1)
				i++;
			else 
				break;
		}
		return forPrint;
	}
}