Cod sursa(job #1345788)

Utilizator AndlolAndrei Apostoiu Andlol Data 17 februarie 2015 21:13:07
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.99 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cdl;

//import java.io.BufferedReader;
import java.io.FileInputStream;
//import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) throws IOException  {
        
        Scanner reader = new Scanner(new FileInputStream("convertor.in"));
        //BufferedReader br = new BufferedReader(new FileReader("convertor.in"));
        PrintWriter out = new PrintWriter("convertor.out");
        //BufferedWriter out= new BufferedWriter(new FileWriter("convertor.out"));
        

    StringBuilder sb = new StringBuilder();
    //String line = br.readLine();
    String line = reader.nextLine();
    
    int comma =-1;
    int rightCurly = 0;
    int douaPcte;
    
    int position1 = 0;
    int position2 = 0;
    String text1="";
    
    int lastComma=0;
   //reader.hasNextLine() //
    while( reader.hasNextLine() ) {
      comma=-1;
      rightCurly = line.indexOf('}');  
      if (rightCurly == -1 ) {  
        lastComma=line.lastIndexOf(',');
        while(comma != lastComma){
       
        position1 = line.indexOf('"',comma+1);
        position2 = line.indexOf('"',position1+1);
        comma = line.indexOf(',',comma+1);
        
        out.write(line.substring(position1+1, position2)+"," );
        
        System.out.print(line.substring(position1+1, position2)+",");
       }
        
      }
      else {
      
       while(comma < rightCurly){
           position1 = line.indexOf('"',comma+1);
           position2 = line.indexOf('"',position1+1);
           comma = line.indexOf(',',comma+1);
           
           out.write(line.substring(position1+1, position2)+"," );
           System.out.print(line.substring(position1+1, position2)+"," );
           
       }   
      //out.newLine();
       out.println();
       System.out.println();
      
      break;
      }
       //line = br.readLine(); 
       line = reader.nextLine(); 
    }
 
    //br = new BufferedReader(new FileReader("file.txt"));
    reader = reader = new Scanner(new FileInputStream("convertor.in"));
    line = reader.nextLine();
   // line = br.readLine();
    
   
    while( line != null ) {
        
        lastComma=line.lastIndexOf(',');
        comma=-1;
        while(comma < lastComma){
        
            rightCurly=line.indexOf('}',comma+1);
            douaPcte=line.indexOf(':',comma+1);
        
            position1 = douaPcte+1;
            comma=line.indexOf(',',douaPcte);
            position2 = comma;
            
            
            text1=line.substring(position1+1, position2).trim() ;
            
            if(text1.charAt(0)=='"') {
                if(text1.charAt(text1.length()-1)=='"') {
                    out.write(text1.substring(1,(text1.length()-1) ) +",");
                    System.out.print(text1.substring(1,(text1.length()-1) ) +",");
                }
                else{
                    text1=text1.substring(1,(text1.length()-1) ).trim();
                    out.write(text1.substring(0,text1.length()-1)+"," );
                    System.out.print(text1.substring(0,text1.length()-1)+"," );
            
                }
            }
            else {
            out.write((line.substring(position1+1, position2).trim())+"," );
            System.out.print((line.substring(position1+1, position2).trim())+"," );
            }
            if(rightCurly != -1 && comma > rightCurly) {
                //out.newLine();
                out.println();
                System.out.println();
            }
        }
        int rightSquare=line.lastIndexOf(']'); 
        
        if( rightSquare!= -1) {
            douaPcte=line.indexOf(':',comma+1);
        
            //position1 = douaPcte+1;
            comma=line.lastIndexOf('}');
           // position2 = comma;
            //position1+1
            text1=line.substring(douaPcte+2, comma).trim() ;
            
            if(text1.charAt(0)=='"') {
                if(text1.charAt(text1.length()-1)=='"') {
                    out.write(text1.substring(1,(text1.length()-1) )+"," );
                    
                }
                else{
                    text1=text1.substring(1,(text1.length()-1) ).trim();
                    out.write(text1.substring(0,text1.length()-1) +",");
                  
                }
            }
            else {//position1_1
                out.write((line.substring(douaPcte+2, comma).trim())+"," );
                
            }
        }
        
        //line = br.readLine();
       if(reader.hasNextLine()){
        line = reader.nextLine();
       }
       else break;
    }  
    
    out.close();
    //br.close();
    reader.close();
     
    }
    
}