Pagini recente » Cod sursa (job #2773537) | Cod sursa (job #3165131) | Cod sursa (job #571210) | Cod sursa (job #747910) | Cod sursa (job #1361783)
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();
}
}
}