Pagini recente » Cod sursa (job #111395) | Cod sursa (job #1788675) | Cod sursa (job #129169) | Cod sursa (job #2534833) | Cod sursa (job #1340638)
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.hasNext())
{
otherRows += reader2.nextLine();
}
int countBrackets = 0;
for(int i = 0; i < otherRows.length(); i++)
{
if(otherRows.charAt(i) == '}')
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++)
{
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("\""))
writer.write(otherRows.substring(c + 1, d - 1).substring(1, otherRows.substring(c + 1, d - 1).lastIndexOf("\"")) + ",");
else
writer.write(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("\""))
writer.write(otherRows.substring(c + 1, e - 1).substring(1, otherRows.substring(c + 1, e - 1).lastIndexOf("\"")) + ",");
else
writer.write(otherRows.substring(c + 1, e - 1) + ",");
}
}
System.out.println();
}
writer.close();
reader2.close();
}
}