Cod sursa(job #1340718)

Utilizator raduneagoeNeagoe Radu raduneagoe Data 11 februarie 2015 23:31:30
Problema Convertor Scor 20
Compilator java Status done
Runda rosedu_cdl_2015 Marime 2.94 kb
import java.io.*;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		Scanner reader = new Scanner(new FileInputStream("convertor.in"));
		PrintWriter writer = new PrintWriter("convertor.out");

		String firstRow = new String();
		int count = 0;
		while (!reader.hasNext("},"))
		{
			firstRow += reader.next().toString() + " ";
		}

		int a = 0, b = 0;
		while (a < firstRow.lastIndexOf(":") && b < firstRow.lastIndexOf("\""))
		{
			a = firstRow.indexOf(":", a) + 1; // primele ":" pt a stii care este
												// sfarsitul substringului
			b = firstRow.indexOf("\"", b) + 1;// primele ghilimele pt a stii
												// care este inceputul
												// substrinigului
			if (firstRow.substring(b, a - 2).endsWith("\""))
				writer.write(firstRow.substring(b, a - 3) + ",");
			else
				writer.write(firstRow.substring(b, a - 2) + ",");
			count++;
			// pot aparea inca 3 ghilimele daca urmeaza un string dupa ":"
			// sau doar 1 ghilimea daca urmeaza un numar dupa cele ":"
			b = firstRow.indexOf("\"", b) + 1;
			if (firstRow.startsWith("\"", a + 1) || firstRow.startsWith("\"", a))
			{
				b = firstRow.indexOf("\"", b) + 1;
				b = firstRow.indexOf("\"", b) + 1;
			}
		}
		reader.close();

		Scanner reader2 = new Scanner(new FileInputStream("convertor.in"));
		String otherRows = new String();
		while (reader2.hasNextLine())
		{
			otherRows += reader2.nextLine();
		}
		
		int countBrackets = 0;
		for(int i = 0; i < otherRows.length(); i++)
		{
			if(otherRows.charAt(i) == '}')
				countBrackets++;
		}

		String [] str = new String[countBrackets];
		int c = 0, d = 0, e = 0;//stiu ca nu au nume prea sugestiv dar nu am idee cum sa le numesc
		for(int i = 0; i < countBrackets; i++)
		{
			str[i] = new String();
			writer.write("\n");
			for (int j = 0; j < count; j++)
			{
				c = otherRows.indexOf(":", c) + 1;
				d = otherRows.indexOf(",", d) + 1;
				e = otherRows.lastIndexOf("}") + 1;
				
				//daca dupa ":" se afla doua spatii, cresc de unde incepe substringul
				if(otherRows.startsWith(" ", c + 1))
					c++;
				
				try
				{
					if (otherRows.substring(c + 1, d - 1).contains("\""))
						str[i] += otherRows.substring(c + 1, d - 1).substring(1, otherRows.substring(c + 1, d - 1).lastIndexOf("\"")) + ","; 
					else
						str[i] += otherRows.substring(c + 1, d - 1) + ",";
					//la ultimul cuvant nu am "," si imi va da eroare pe care o prind si o tratez
				}catch (Exception exception)
				{
					if (otherRows.substring(c + 1, e - 1).contains("\""))
						str[i] += otherRows.substring(c + 1, e - 1).substring(1, otherRows.substring(c + 1, e - 1).lastIndexOf("\"")) + ","; 
					else
						str[i] += otherRows.substring(c + 1, e - 1) + ","; 
				}
			}
			if(str[i].contains("}"))
				str[i] = str[i].substring(0, str[i].length() - 2) + ",";
			writer.write(str[i]);
		}
		
		writer.close();
		reader2.close();
	}
}