Cod sursa(job #1345565)

Utilizator AndlolAndrei Apostoiu Andlol Data 17 februarie 2015 18:48:50
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.24 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.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 *
 * @author Andrei
 */
public class Main {

    public static void main(String[] args) throws Exception  {
        
        BufferedReader br = new BufferedReader(new FileReader("convertor.in"));
        PrintWriter out = new PrintWriter("convertor.out");
        //BufferedWriter out= new BufferedWriter(new FileWriter("convertor.out"));
        
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    
    int comma =-1;
    int rightCurly = 0;
    int douaPcte;
    
    int position1 = 0;
    int position2 = 0;
    String text1="";
    
    int lastComma=0;
   
    while( line != null ) {
      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.append(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.append(line.substring(position1+1, position2)+"," );
           
           
       }   
      //out.newLine();
       out.println();
     
      
      break;
      }
       line = br.readLine();
    }
 
    br = new BufferedReader(new FileReader("file.txt"));
    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.append(text1.substring(1,(text1.length()-1) ) +",");
                    
                }
                else{
                    text1=text1.substring(1,(text1.length()-1) ).trim();
                    out.append(text1.substring(0,text1.length()-1)+"," );
                    
            
                }
            }
            else {
            out.append((line.substring(position1+1, position2).trim())+"," );
            
            }
            if(rightCurly != -1 && comma > rightCurly) {
                //out.newLine();
                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.append(text1.substring(1,(text1.length()-1) )+"," );
                    
                }
                else{
                    text1=text1.substring(1,(text1.length()-1) ).trim();
                    out.append(text1.substring(0,text1.length()-1) +",");
                  
                }
            }
            else {//position1_1
                out.append((line.substring(douaPcte+2, comma).trim())+"," );
                
            }
        }
        
        line = br.readLine();
        
    }  
    
    out.close();
    br.close();
   } catch (Exception e) {}    
    }
    
}