Cod sursa(job #1355614)

Utilizator oana_alexandra.orjanuOrjanu Oana-Alexandra oana_alexandra.orjanu Data 22 februarie 2015 21:37:49
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.94 kb

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
 * 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.
 */
/**
 *
 * @author Elesah
 */
class Main
{
    public static void main (String[] args) throws FileNotFoundException, IOException
    {
        /*Scanner sc = new Scanner (new File("convertor.in"));
        List<String> Lines = new ArrayList<String>();
        String keeper = new String("");
        // sc.useDelimiter("\\n");
        /*String aux = new String();
        aux = sc.nextLine();
        while (sc.hasNextLine())
        {    
            Lines.add(sc.nextLine());
            sc = sc.nextLine();
//        Lines.add(sc.next());
        sc.close();
        for (int i = 0; i < Lines.size() - 1;i++ )
        {
            
            System.out.println(Lines.get(i)); 
            keeper = keeper + Lines.get(i);
        }*/
        String keeper = new Scanner( new File("convertor.in") ).useDelimiter("\\A").next();
        
       // System.out.println(keeper);
       // keeper.replaceAll("\\s+","");
        //System.out.println(keeper); 
        char[] store = keeper.toCharArray();
        //String[] auxes = new String[store.length];
       
        List<String> auxes = new ArrayList<String>(store.length);
        List<String> values = new ArrayList<String>(store.length);
        List<String> keys = new ArrayList<String>(store.length);
        List<String> actuals = new ArrayList<String>(store.length);
        //String[] values = new String[store.length];
        for (int i = 0; i < store.length - 1; i++)
        {    
            auxes.add(i,new String());
            actuals.add(i, new String());
          //  keys[i] = new String("");
           // values[i] = new String("");
        }    

            int j = 0;
         //    for (int i = 0;i <  store.length - 1;i++)
          //     System.out.println(store[i]);
        for (int i = 0;i <  store.length - 1;i++)
         //   System.out.println(store[i]);
      {
           // System.out.println(store[i]);
            if (/*(store[i]!=' ')&&*/ (store[i]!='\n')&&(store[i]!='\"') && (store[i]!=']') && (store[i]!='}')  && (store[i]!='{')&& (store[i]!='[') &&(store[i]!=',') &&(store[i] != ':'))
            {
              //  System.out.println("*");
                //if(store[i] == ' ')
                  //  System.out.println("spaaace");
                
                auxes.set(j, auxes.get(j)+store[i]);
                
            }
            else
            {  
                //System.out.println(auxes.get(j));
                    j++;
                //System.out.println(i);
            }
        }    
        /*for (int i = 0; i < auxes.size()-1; i++)
        {
            if(auxes.get(i).compareTo("")==0)
                auxes.remove(i);
        }
          */
        j = 0;
             //for (int i = 0; i < auxes.size()-1; i++)
             //System.out.println(auxes.get(i));
        for (int i = 0; i < auxes.size()-1; i++)
        {
            if(auxes.get(i).trim().compareTo("")!= 0)
            { 
                actuals.set(j, auxes.get(i));
                j++;
            }
        }    
         for (int i = 0; i < actuals.size()-1; i++)
         { 
             actuals.set(i, actuals.get(i).replaceAll("^\\s+|\\s+$", ""));
             
    //         System.out.println(actuals.get(i));
         }    
        
        j = 0;
        int k = 0;
        for (int i = 0; i < actuals.size() - 1; i++)
        {
            if (i%2 == 0 && keys.contains(actuals.get(i))==false )
            {    
                
                keys.add(j,actuals.get(i));
                j++;
            }
            else
                if (i%2 == 1)
                {
          //          actuals.get(i).replaceAll("\\s+$", "");
                    values.add(k, actuals.get(i));
                    k++;
                }    
        }
       //for (int i = 0; i < values.size(); i++)
         //   System.out.println(values.get(i));
        File out = new File("convertor.out");
        out.createNewFile();
        PrintWriter pr = new PrintWriter(out);
        for (int i = 0; i < keys.size() - 1; i++)
       
             
            pr.print(keys.get(i)+",");
       pr.println();
        
        k = 0;
        while (values.get(k).trim().compareTo("") != 0)
        {
             values.set(k, values.get(k).replaceAll("\\s+$", ""));
           pr.print(values.get(k)+",");
             k++;
            if ( k % (keys.size()-1) == 0 && k!=0)
                pr.println();
           
        }
pr.close();        
            
}
}