Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 10th, 2012  |  syntax: None  |  size: 2.25 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.File;
  2. import java.util.Scanner;
  3. import java.util.Formatter;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6.  
  7. public class Munge
  8. {
  9.  
  10.     private String inFileName, outFileName;
  11.     private Scanner inFile;
  12.     private Formatter outFile;
  13.     private int line = 0;
  14.  
  15.     private String[] data = new String[100];
  16.  
  17.     public Munge(String inFileName, String outFileName)
  18.     {
  19.         this.inFileName = inFileName;
  20.         this.outFileName = outFileName;
  21.     }
  22.  
  23.     public void openFiles()
  24.     {
  25.         try
  26.         {
  27.             inFile = new Scanner(new File(inFileName));
  28.         }
  29.         catch(FileNotFoundException exception)
  30.         {
  31.             System.err.println("File not found.");
  32.             System.exit(1);
  33.         }
  34.         catch(SecurityException exception)
  35.         {
  36.             System.err.println("You do not have access to this file.");
  37.             System.exit(1);
  38.         }
  39.  
  40.         try
  41.         {
  42.             outFile = new Formatter(outFileName);
  43.         }
  44.         catch(FileNotFoundException exception)
  45.         {
  46.             System.err.println("File not found.");
  47.             System.exit(1);
  48.         }
  49.         catch(SecurityException exception)
  50.         {
  51.             System.err.println("You do not have access to this file.");
  52.             System.exit(1);
  53.         }
  54.     }
  55.  
  56.     public void readRecords()
  57.     {
  58.         while(inFile.hasNext())
  59.         {
  60.             data[line] = inFile.nextLine();
  61.             System.out.println(data[line]);
  62.             line++;
  63.         }
  64.     }
  65.  
  66.     public void writeRecords()
  67.     {        
  68.         for(int i = 0; i < line; i++)
  69.         {
  70.             String tokens[] = data[i].split(", ");
  71.             String city = tokens[i];
  72.             String province = tokens[i + 1];
  73.            
  74.             ArrayList<ArrayList<String>> provinces = new ArrayList<ArrayList<String>>();
  75.            
  76.             if(!provinces.contains(province))
  77.                 provinces.add(province);
  78.            
  79.             provinces(indexOf(province)).add(new ArrayList(cities));
  80.            
  81.         }
  82.     }
  83.  
  84.     public void closeFiles()
  85.     {
  86.         if(inFile != null)
  87.             inFile.close();
  88.  
  89.         if(outFile != null)
  90.             outFile.close();
  91.     }
  92. }