Pagini recente » Cod sursa (job #296956) | Cod sursa (job #713562) | Cod sursa (job #1363540) | Cod sursa (job #1974595) | Cod sursa (job #1341570)
import java.io.*;
import java.util.Scanner;
public class Main2
{
public static int charAppears(String s, char ch)
{
int counter = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == ch)
counter++;
}
return counter;
}
public static String deleteChar(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!(s.startsWith(" ") || s.startsWith("\"") || s.endsWith(" ") || s.endsWith("\"") || s.endsWith("}") || s.endsWith(",") || s.endsWith("},")))
break;
if(s.startsWith(" ") || s.startsWith("\""))
{
s = s.substring(1);
i = 0;
}
if(s.endsWith(" ") || s.endsWith("\"") || s.endsWith("}") || s.endsWith(",") || s.endsWith("},"))
{
s = s.substring(0,s.length() - 1);
i = 0;
}
}
return s;
}
public static void main(String[] args) throws IOException
{
Scanner reader = new Scanner(new FileInputStream("convertor.in"));
PrintWriter writer = new PrintWriter("convertor.out");
String JSON = new String(), str = new String();
while (reader.hasNextLine())
{
JSON += reader.nextLine();
}
reader.close();
// primul rand
int a = JSON.indexOf("\"") + 1, b = JSON.indexOf(":") - 1;
int n = charAppears(JSON.substring(JSON.indexOf(':'), JSON.indexOf('}') + 1), ',') + 1;//nr virgulelor
for (int i = 0; i < n; i++)
{
str = JSON.substring(a, b);
str = deleteChar(str);
writer.write(str + ",");
a = JSON.indexOf(",", a) + 1;
b += 2;
b = JSON.indexOf(":", b) - 1;
}
//urmatoarele randuri pe care le aflu dupa numarul de paranteze inchise '}'
int m = charAppears(JSON, '}');
a = b = 0;
for(int i = 0; i < m; i++)
{
writer.write(System.getProperty("line.separator"));
try //la ultimul rand nu voi mai avea ',' dupa ultima paranteza
{
for(int j = 0; j < n; j++)
{
a = JSON.indexOf(':', a) + 1;
b = JSON.indexOf(',', b) + 1;
str = JSON.substring(a, b);
str = deleteChar(str);
writer.write(str + ",");
}
}catch (Exception e)
{
b = JSON.lastIndexOf('}');
str = JSON.substring(a, b);
str = deleteChar(str);
writer.write(str + ",");
}
}
writer.close();
}
}