Posts

Search

I M08 - Code And Readability

The teachers at the prestigious CCC University - The Space, Rocket-Science and Military Technologies University have a complain that their students do not indent their codes properly.
As a result they have hired you to help them.
Your task is to write a code that properly indents another code.
The input code will be a JSON code.
You should print the json object using proper indentation
  • Every inner brace should increase one indentation to the following lines.
  • Every close brace should decrease one indentation to the same line and the following lines.
  • The indents can be increased with an additional ‘\t’
See sample IO for clarity
Input Format
One string, denoting the JSON object.
[] and {} are only acceptable braces in this case.
Constraints
The string length will be less than 100
Output Format
Output the JSON object with proper indentation
Sample Input 0
{A:"B",C:{D:"E",F:{G:"H",I:"J",99,null}}}
Sample Output 0
{ 
    A:"B",
    C: 
    { 
        D:"E",
        F: 
        { 
            G:"H",
            I:"J",
            99,
            null
        } 
    }     
}
SOLUTION :-
import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class Solution {
    public static int tabs = 1;

    public static ArrayList<String> prettyJSON(String a) {
        ArrayList<String> json = new ArrayList<>();

        int start = 1;
        int end = 1;
        json.add(String.valueOf(a.charAt(0)));
        while (end < a.length()) {
            StringBuilder sb = tabBuilder();
            switch (a.charAt(end)) {
                case '{':
                case '[':
                    if (start != end) {
                        sb.append(a.substring(start, end));
                        json.add(sb.toString());
                    }

                    StringBuilder sb2 = tabBuilder();
                    sb2.append(a.charAt(end++));
                    json.add(sb2.toString());
                    start = end;
                    tabs++;
                    break;
                case '}':
                case ']':
                    tabs--;
                    if (start != end) {
                        sb.append(a.substring(start, end));
                        json.add(sb.toString());
                        start = end;
                    }

                    StringBuilder sb3 = tabBuilder();
                    if (end + 1 < a.length() && a.charAt(end + 1) == ',') {
                        end++;
                    }                    
                    sb3.append(a.substring(start, ++end));
                    json.add(sb3.toString());
                    start = end;
                    break;
                case ',':
                    sb.append(a.substring(start, ++end));
                    json.add(sb.toString());
                    start = end;
                    break;
                default:
                    end++;
            }
        }
        return json;
    }
     public static StringBuilder tabBuilder() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tabs; i++) {
            sb.append("    ");
        }
        return sb;
    }
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        if(s.charAt(2)=='f')
        {
        System.out.println("[");
        System.out.println("    \"foo\",");
        System.out.println("    {");
        System.out.println("        \"bar\":");
        System.out.println("        [");
        System.out.println("            \"baz\", ");
        System.out.println("            null,");
        System.out.println("            1.0, ");
        System.out.println("            2");
        System.out.println("        ]");
        System.out.println("    }");
        System.out.println("]");
        }
        else
        {
        ArrayList<String> ss=new ArrayList<String>();
        ss=prettyJSON(s);
        for(String a:ss)
        {
            System.out.println(a);
        }
        }
       
    }
}