fork(14) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.*;
  4. import org.json.simple.JSONArray;
  5. import org.json.simple.JSONObject;
  6. import org.json.simple.parser.JSONParser;
  7. import org.json.simple.parser.ParseException;
  8.  
  9. import com.google.gson.Gson;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static String SPACE = " ";
  15. static Gson gson = new Gson();
  16. static Json3 innerchild = new Json3();
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. String line = "0 senior 3 excellent 4 high no";
  20. JSONParser parser = new JSONParser();
  21. JSONObject json = null;
  22. Json2 leaf = new Json2();
  23. Json3 child = new Json3();
  24. String[] parts = line.split(" ");
  25. int len = parts.length;
  26. String leafNode = parts[len - 1];
  27. child.setAttributeIndex(Integer.parseInt(parts[0]));
  28. child.setAttributeName(parts[1]);
  29. JSONArray arr = new JSONArray();
  30. if (len == 3) {
  31. leaf.setAttribute(leafNode);
  32. leaf.setLeaf(true);
  33. leaf.setChildren(arr);
  34. json = (JSONObject) parser.parse(gson.toJson(leaf));
  35. child.setChildren(json);
  36.  
  37. } else {
  38. for (int i = 2; i < parts.length - 1; i++) {
  39. if (len != i) {
  40. if (i == 3) {
  41. innerchild.setAttributeIndex(Integer.parseInt(parts[i - 1]));
  42. innerchild.setAttributeName(parts[i]);
  43. leaf.setAttribute(leafNode);
  44. leaf.setLeaf(true);
  45. json = (JSONObject) parser.parse(gson.toJson(leaf));
  46. innerchild.setChildren(json);
  47. arr.add(innerchild);
  48. leaf.setChildren(arr);
  49. leaf.setLeaf(false);
  50. } else {
  51. leaf.setAttribute(parts[i]);
  52. leaf.setChildren(arr);
  53. json = (JSONObject) parser.parse(gson.toJson(leaf));
  54. child.setChildren(json);
  55. }
  56. }
  57. }
  58. }
  59. if(parts.length != 3){
  60. arr = new JSONArray();
  61. arr.add(innerchild);
  62. leaf.setChildren(arr);
  63. json = (JSONObject) parser.parse(gson.toJson(leaf));
  64. child.setChildren(json);
  65. }
  66.  
  67. System.out.println(gson.toJson(child));
  68. }
  69. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
Main.java:4: error: package org.json.simple does not exist
import org.json.simple.JSONArray;
                      ^
Main.java:5: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
                      ^
Main.java:6: error: package org.json.simple.parser does not exist
import org.json.simple.parser.JSONParser;
                             ^
Main.java:7: error: package org.json.simple.parser does not exist
import org.json.simple.parser.ParseException;
                             ^
Main.java:9: error: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
Main.java:15: error: cannot find symbol
	static Gson gson = new Gson();
	       ^
  symbol:   class Gson
  location: class Ideone
Main.java:16: error: cannot find symbol
	static Json3 innerchild = new Json3();
	       ^
  symbol:   class Json3
  location: class Ideone
Main.java:15: error: cannot find symbol
	static Gson gson = new Gson();
	                       ^
  symbol:   class Gson
  location: class Ideone
Main.java:16: error: cannot find symbol
	static Json3 innerchild = new Json3();
	                              ^
  symbol:   class Json3
  location: class Ideone
Main.java:20: error: cannot find symbol
		JSONParser parser = new JSONParser();
		^
  symbol:   class JSONParser
  location: class Ideone
Main.java:20: error: cannot find symbol
		JSONParser parser = new JSONParser();
		                        ^
  symbol:   class JSONParser
  location: class Ideone
Main.java:21: error: cannot find symbol
		JSONObject json = null;
		^
  symbol:   class JSONObject
  location: class Ideone
Main.java:22: error: cannot find symbol
		Json2 leaf = new Json2();
		^
  symbol:   class Json2
  location: class Ideone
Main.java:22: error: cannot find symbol
		Json2 leaf = new Json2();
		                 ^
  symbol:   class Json2
  location: class Ideone
Main.java:23: error: cannot find symbol
		Json3 child = new Json3();
		^
  symbol:   class Json3
  location: class Ideone
Main.java:23: error: cannot find symbol
		Json3 child = new Json3();
		                  ^
  symbol:   class Json3
  location: class Ideone
Main.java:29: error: cannot find symbol
		JSONArray arr = new JSONArray();
		^
  symbol:   class JSONArray
  location: class Ideone
Main.java:29: error: cannot find symbol
		JSONArray arr = new JSONArray();
		                    ^
  symbol:   class JSONArray
  location: class Ideone
Main.java:34: error: cannot find symbol
			json = (JSONObject) parser.parse(gson.toJson(leaf));
			        ^
  symbol:   class JSONObject
  location: class Ideone
Main.java:45: error: cannot find symbol
							json = (JSONObject) parser.parse(gson.toJson(leaf));
							        ^
  symbol:   class JSONObject
  location: class Ideone
Main.java:53: error: cannot find symbol
						json = (JSONObject) parser.parse(gson.toJson(leaf));
						        ^
  symbol:   class JSONObject
  location: class Ideone
Main.java:60: error: cannot find symbol
			arr = new JSONArray();
			          ^
  symbol:   class JSONArray
  location: class Ideone
Main.java:63: error: cannot find symbol
			json = (JSONObject) parser.parse(gson.toJson(leaf));
			        ^
  symbol:   class JSONObject
  location: class Ideone
23 errors
stdout
Standard output is empty